Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:48:18+00:00 2026-06-09T02:48:18+00:00

I have the following linq select query that loops round all the ‘Places’ linked

  • 0

I have the following linq select query that loops round all the ‘Places’ linked to ‘adrianMember’ that start with B, I only want to show the PlaceName. I have a navigation association from Member to Place but not from Place to Member.

using (var fdb = new FALDbContext())
{
    var adrianMember = fdb.Members.Find(1);

    foreach (string s in adrianMember.Places.Where(p=>p.PlaceName.StartsWith("B")).Select(p => p.PlaceName))
    {
        Console.WriteLine("- " + s);
    }
}

I have experimented with various linq syntax too, for example not using Find…

var adrianMember = fdb.Members.Where(m => m.MemberId == 1).FirstOrDefault();

and providing two linq queries, one to retrieve the member and then later retrieve the related places (and hopefully making the EF do some lazy deferred loading) but that still results in very inefficient sql.

using (var fdb = new FALDbContext())
{
    //Need the FirstOrDefault otherwise we will return a collection (first or default will return the inner collection
    //could have multiple members with multiple places
    var members = fdb.Members.Where(m=>m.FirstName == "Bob");

    foreach (var member in members)
    {
        var places = member.Places.Where(p => p.PlaceName.StartsWith("B")).Select(p => p.PlaceName);

        foreach (var place in places)
        {
            Console.WriteLine(place);
        }
    }
}

The SQL output gets all rows and all columns

exec sp_executesql N'SELECT 
[Extent1].[PlaceId] AS [PlaceId], 
[Extent1].[PlaceName] AS [PlaceName], 
[Extent1].[PlaceLocation] AS [PlaceLocation], 
[Extent1].[Member_MemberId] AS [Member_MemberId]
FROM [dbo].[Places] AS [Extent1]
WHERE [Extent1].[Member_MemberId] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1

Is there a way to restrict the sql to something like

SELECT PlaceName FROM Places WHERE MemberId = 1 AND PlaceName like ‘B%’

I have a couple of situations in my project where the above generated sql will make the query too slow (20,000 records per member over 20 columns). Is linq clever enough to make the change if I do have more records?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:48:20+00:00Added an answer on June 9, 2026 at 2:48 am

    After a month away from this problem I thought I’d take a second look…

    Firstly, I’m not really sure what I wanted to achieve!!! However, I do seem to have found some interesting and more efficient sql queries for what I wanted.

    My first option is to use explicit loading using the Entry & Collection methods. This creates a SQL query to retrieve each corresponding Place record and, importantly, projects the columns I want (just the description) and restricts the rows (those Places beginning with L). This does create many queries though if I have many associated places but I can do this on the fly if I don’t know which Places I want to retrieve.

        using (var fdb = new FALDbContext())
        {
            foreach (var member in fdb.Members)
            {
                var places = fdb.Entry(member)
                                .Collection(m => m.Places)
                                .Query()
                                .Where(p => p.PlaceName.StartsWith("L"))
                                .Select(p => p.PlaceName);
    
                foreach (var place in places)
                {
                    Console.WriteLine(place);
                }
            }
        }
    

    The second option is to use lazy loading but specify a tightly controlled bit of LINQ to Entities.

    This goes to the database once and retrieves just the members I want and projects and restricts both tables and all in one lovely efficient looking sql query!

        using (var fdb = new FALDbContext())
        {
            var myList = fdb.Members
                                .Where(m => m.GenderShareWithId > 0)
                                .Select(m => new { m.MemberId, m.DisplayName, Places = m.Places.Where(p => p.PlaceName.StartsWith("L")).Select(p => p.PlaceName) });
    
            foreach (var item in myList)
            {
                Console.WriteLine(item.DisplayName);
                foreach (var place in item.Places)
                {
                    Console.WriteLine(" - " + place);
                }
            }
        }
    
    
    SELECT 
        [Extent1].[MemberId] AS [MemberId], 
        [Extent1].[DisplayName] AS [DisplayName], 
        [Extent2].[PlaceName] AS [PlaceName]
    FROM  [dbo].[Members] AS [Extent1]
    LEFT OUTER JOIN [dbo].[Places] AS [Extent2] 
        ON ([Extent1].[MemberId] = [Extent2].[Member_MemberId]) AND ([Extent2].[PlaceName] LIKE N'L%')
    WHERE [Extent1].[GenderShareWithId] > 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following LINQ query that selects all the pages that have a
I have the following LINQ query: var queryGroups = (from p in db.cl_contact_event select
I have to following SQL Statement that I want to conver to LINQ Select
I have the following LINQ query that I need to translate to Entity SQL
i have the following linq query that create a left join between two tables:
I have the following LINQ query that returns two objects from my database. These
I have the following query that I would like to convert to LINQ to
I have the following LINQ query that I am using to construct a structure
So I have the following linq query that times out when the foreach loop
I have a LINQ query that looks like the following: DateTime today = DateTime.UtcNow;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.