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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:15:29+00:00 2026-05-24T07:15:29+00:00

With LINQ, how to do a select and only return the first associated child

  • 0

With LINQ, how to do a select and only return the first associated child item?

I have:

[table: Report]
    GeneratedDate
    PerformanceMetric  1 -----> N [table PerformanceMetric]
    Bios                              Timestamp
    ...                               X
                                      Y 
                                      ..... (there is a good dozen other fields)

Currently, I do:

 Report = _ctx.Reports.SingleorDefault(rpt => rpt.bios == mySerial);

This returns the report with all the PerformanceMetric associated. I would like to only have the first occurrence of PerformanceMetric returned.

Does LINQ provide a built-in way of doing so?

EDIT: In fact, I am looking for the Report, but the PerformanceMetric should only contain the first element. (Report.PerformanceMetrics.Count <= 1 )
sry for not being clear..

  • 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-24T07:15:31+00:00Added an answer on May 24, 2026 at 7:15 am

    Hard to tell exactly the full structure based on your XML, but something like:

    var firstMetric = _ctx.Reports
        .Where(rpt => rpt.bios == mySerial)
        .Select(rpt => rpt.PerformanceMetrics) // assuming name here...
        .FirstOrDefault();
    

    This returns the first performance metric of the report matching mySerial, or default (null for classes) if the where clause didn’t find any reports or no performance metrics existed.

    Update: based on the update in the question, you want something different. You want the report, but with only 1 of the performance metrics. This involves mutating the original report (not sure of all the members), which can be kinda hairy because you don’t want to manipulate your original data in LINQ.

    So you have two options:

    1. You can create a new instance of Report and copy all the properties and just one metric.
    2. You can create an anonymous type with the report and the property together.

    I’d probably prefer #2 since returning a modified Report might be confusing. So you could do:

    var firstMetric = _ctx.Reports
        .Where(rpt => rpt.bios == mySerial)
        .Select(new { Report = rpt, Metric = rpt.PerformanceMetrics.FirstOrDefault() })
        .FirstOrDefault();
    

    This will scan the Reports for the one where bios == mySerial, create a new anonymous type with a Report member == to the original report, and a Metric member == the first metric in the Report list.

    If no metrics exist but report exists, you’ll get an anonymous type with Report = your report and Metric = null. If no report exists matching condition, returns null.

    If you do really want #1, you could do this:

    var firstMetric = _ctx.Reports
        .Where(rpt => rpt.bios == mySerial)
        .Select(new Report 
            { 
                PerformanceMetrics = rpt.PerformanceMetrics.Take(1),
                // copy all other Report fields here...
            })
        .FirstOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I select multiple columns in linq to sql method syntax? I only
I have to following SQL Statement that I want to conver to LINQ Select
How to use Linq to select and group complex child object from a parents
Say I have a linq query select r in db.Roles where r.RoleName DOES NOT
This is probably a basic LINQ question. I have need to select one object
I have a Parent/Child Table relationship with the ChildTable linking back to the ParentTable
I have an EF4.1 database first application, and the .ToString() method is only giving
I would like to select only few columns from a certain (Blobs) table. I
I have an array of strings, and a Linq query: /// <summary> /// Return
What is equal of below sql in LINQ select MIN(finishTimestamp) AS FromDate, MAX(finishTimeStamp) AS

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.