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 6914209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:19:03+00:00 2026-05-27T09:19:03+00:00

I have code like this var results = (from c in Customers join o

  • 0

I have code like this

var results =   (from c in Customers  
                    join o in Orders  
                    on c.Id equals o.CustomerId  
                    join p in Products  
                    on p.Id equals o.ProductId  
                    select new   
                    {  
                        CustomerId = c.Id,     // this is a GUID  
                        OrderId = o.Id,        // this is a GUID    
                        ProductName = p.ProductName,  
                     }).ToList();  

Let’s say I want to get a list of all customer Ids that orders a product that has name = foo
My problem is that because its an anonymous type, how can I refer product name in any Linq query that I want to run on results?

  • 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-05-27T09:19:04+00:00Added an answer on May 27, 2026 at 9:19 am
    var filteredResults = results.Where(r => r.ProductName == "X");
    

    The compiler’s type inference takes care of it for you. The complete answer to your question:

    var customerIds = results
        .Where(r => r.ProductName == "X")
        .Select(r => r.CustomerId)
        .Distinct()
        .ToList();
    

    or

    var customerIds = (from r in results
                      where r.ProductName == "X"
                      select r.CustomerId)
        .Distinct()
        .ToList();
    

    EDIT

    Some musings on type inference

    To select the lengths from a sequence of strings called list, you can call Select either using classic static method syntax or as an extension method:

    Enumerable.Select<string, int>(list, s => s.Length)
    list.Select<string, int>(s => s.Length)
    

    Thanks to type inference, you don’t need the type arguments:

    Enumerable.Select(list, s => s.Length)
    list.Select(s => s.Length)
    

    In this case, the compiler can prove that the type arguments are string and int by looking at the method arguments, and it supplies these type arguments on your behalf without your having to type them into the source code.

    For anonymous types, you can’t provide the first type argument, because the type doesn’t have a name for you to use in the source code (that’s what “anonymous” means, after all: “without a name”). (You can see therefore that anonymous types and type inference were both critical — and closely related — prerequisites to implementing linq in the first place.)

    If you check out the IL for the anonymous type example above, you’ll see that the compiler has in fact given the type a name (which contains characters that are illegal in C# identifiers). When you call Select, the compiler deduces from the type of the enumerable (IEnumerable<CrazilyNamedAnonymousType>) that the first type argument should be the anonymous type, and, as with the string example, it supplies that value on your behalf.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq to Entities query like this one: var results = from
I have a query linq like this: var query = from c in Context.Customers
I have a query that looks like this: var results = from person where
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
This is a really basic question but... I have some code like this var
If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName)
I have some LINQ code that generates a list of strings, like this: var
When we have code like: function a(){ var x =0; this.add=function(){ alert(x++); } }
I have a Linq2Sql query that looks like this: var data = from d

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.