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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:47:14+00:00 2026-06-10T23:47:14+00:00

I’m trying to convert the following to a LINQ to SQL statement in C#.

  • 0

I’m trying to convert the following to a LINQ to SQL statement in C#. Can anyone give me a hand? Basically my table keeps record of all history of changes such that the created date max date for each seedlot is the most recent record and the correct one to show.

SELECT 
    reports.* 
FROM
    [dbo].[Reports] reports
WHERE
    reports.createdDate
IN (
    SELECT 
        MAX(report_max_dates.createdDate) 
    FROM 
        [dbo].[Reports] report_max_dates
    GROUP BY 
        report_max_dates.Lot
    )

So far this is what I have.

var result = (from report in db.Reports
    where report.createdDate == (from report_max in db.Reports
                                group report_max by report_max.Lot into report_max_grouped
                                select report_max_grouped).Max()
    select report);

I can’t figure out how to get the MAX dates for all reports and how to do an IN statement on the report.createdDate.

EDIT

How would I model the statement now If I had a separate title which contains multiple reports. For instance I have reports l,m,n,x,y,z. reports l m n have title “hello” linked to them through a foreign key reportList_Id, and x y c have title “goodbye” linked in the same manner.

Basically I now need to get all the reports into the following object

public class ReportRoot : DbContext {
     public DbSet<ReportList> reportList {get;set;}
}

public class ReportList {
    public int Id {get;set;}
    public string status {get;set;}
    public List<ReportItem> {get;set;}
}

public class ReportItem {
    public int Id {get;set}
    public string title {get;set;}
    public List<Report> {get;set;}
}

public class Report {
    public int Id {get;set;}
    public string Lot {get;set;}
    public DateTime createdDate {get;set;}
}

There is the full listing of my classes. What I need to do is get back the reportList which contains multiple reportItems (which are lists of reports with titles). So these ReportItems will contain the reports themselves. So i need to get back all the reports with max createdDate as we did with the first part of the query but I need to get them back as ReportList objects which contain the ReportItems such that the multiple reports have their titles.

I need the objects in the above format for proper serialization and deserialization of JSON into my objects.

I came up with this, which separates them and gives back titles, but I have unwanted records being returned such as a record that I changed the title for is showing up under both titles.

db.ReportLists.Select(rl => db.ReportItems
                      .Where(ri => ri.ReportListId == rl.Id)
                      .Select(ri => db.Reports
                              .Where(r => r.ReportItemId == ri.Id)
                              .GroupBy(r => r.seedLot)
                              .Select(g => g.OrderByDescending(x => x.createdDate).FirstOrDefault())))

Thansk,
Dman

  • 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-10T23:47:15+00:00Added an answer on June 10, 2026 at 11:47 pm

    I may have bad understood, but basically, you want the last created date for each distinct Lot ?
    If yes, then

    db.Reports
      .GroupBy(r => r.Lot)
      .Select(g => g.OrderByDescending(x => x.createdDate).FirstOrDefault());
    

    or to do like you (but I’m not sure it’s the easiest way)

    var maxDates = Reports.GroupBy(r => r.Lot)
                .Select(x => x.Max(g => g.createdDate).ToList();
    
    var result = db.Reports.Where (m => maxDates.Contains(m.createdDate));
    

    EDIT

    Your example is not that clear (I change a little bit the names for clarity).

    With code first, you should have something like that (idea is the same)

    a class Report

    public class Report {
       public int Id {get;set;}
       public int Lot {get;set;}
       public Datetime CreatedDate {get;set;}
       public virtual Category Category {get;set;} //Navigation property
    }
    

    a class Category

    public class Category {
        public int Id {get;set;}
        public string Title {get;set;}
        public virtual IList<Report> ReportList {get;set;} //Navigation property
    }
    

    and eventually a “result class”

    public class ReportList {
        public string Title {get;set;}
        public List<Report> ReportList {get;set;}
    }
    

    Then query could become

    db.Reports
      .GroupBy(r => r.Lot)
      .Select(g => g.OrderByDescending(x =x.createdDate).FirstOrDefault())
      .GroupBy(m => m.Category.Id)
      .Select(g => new  ReportList {
        Title = g.FirstOrDefault().Category.Title,
        ReportList = g.OrderBy(x => x.Lot)
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.