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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:44:53+00:00 2026-05-17T01:44:53+00:00

I am trying to use LINQ to query a list of objects wherever appropriate.

  • 0

I am trying to use LINQ to query a list of objects wherever appropriate. Currently I am stuck on the syntax of a nested query which I hope you can help me with.

Classes:

public class FooType
{
    public int Id { get; set; }

    public IList<Foo> Foos { get; set; }

}

public class Foo
{
    public int FooTypeId { get; set; }

    public string CorrelationId { get; set; }
}

public class Bar
{
    public string FooCorrelationId { get; set; }
}

Usage:

IList < FooType > fooTypes = new List < FooType >();
// ... add a lot of FooTypes, each enriched with some Foos
Bar bar = new Bar(){FooCorrelationId = "abcdef"};
Foo foo = fooTypes.Where( ft => ft.Foos.Where( f => f.CorrelationId == bar.FooCorrelationId ) ).First<Foo>();

This fails because the outer WHERE-expression is fed with something that does not provide a boolean return value. As you might guess, I intend to something like the following in LINQ:

 foreach (FooType fooType in fooTypes)
 {
     Foo foo = fooType.Foos
                      .Where(f => f.CorrelationId == bar.FooCorrelationId)
                      .First();
 }

Do you have any idea how to get that Foo I’m looking for, the one with CorrelationId “abcdef”?

And one more question for the benchmarkers : What about performance? Does LINQ optimize it’s queries? Or is the result the same as an iteration over the objects like I did in my “something like” block?

Thanks a lot in advance!

  • 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-17T01:44:54+00:00Added an answer on May 17, 2026 at 1:44 am

    Just replace the outer where with a SelectMany and get the first

    Foo foo = fooTypes
        .SelectMany(ft => ft.Foos.Where(f => f.CorrelationId == bar.FooCorrelationId))
        .First();
    

    If you an to get the first Foo across all FooType, what has CorrelationId == FooCorrelationId.

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

Sidebar

Related Questions

I am trying to create Linq Expression that I can use to query child
I'm trying to do a LINQ query to a list of RelatedProducts objects. I
I am trying to use LINQ/LAMBDA access a property List<Latest> latestBooks = DataContext.Session.Query<Book>().Where(x=> x.Enabled
I am trying to use LINQ to return a list of tasks that are
I am trying to use a linq expression to filter a list in a
I am trying to use a dynamic linq query to retrieve an IEnumerable<T> from
Following on from suggestions, I am trying to use List.GetItems(Query) to retrieve my initial
I am trying to use the C# Linq query Descendants method using variables. Here
I am trying to use AddMonths in a query List<Entities.Subscriber> items = (from s
I'm trying to use LINQ to return a list of ids given a list

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.