简体   繁体   English

LINQ查询加入两个表并从表A对应的表B中选择最近的记录

[英]LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A

I have two tables.我有两张桌子。 Table One contains a list of Areas, and Table Two contains a list of Samples, with each Sample row containing Area_ID as a Foreign Key.表一包含一个区域列表,表二包含一个样本列表,每个样本行都包含Area_ID作为外键。

I need to retrieve all the records in my Area table with only the most recent corresponding Sample Status.我需要检索我的 Area 表中的所有记录,并且只使用最新的相应样本状态。 I have this query, but it just returns one Area with the most recent sample from the Sample table:我有这个查询,但它只返回一个带有 Sample 表中最新样本的区域:

var result = (
    from a in db.area
    join c in db.sample
    on a.location_id equals c.location_id

    select new 
    {
        name = a.location_name,
        status = c.sample_status,
        date = c.sample_date
    }).OrderByDescending(c => c.date).FirstOrDefault();

A solution could be filtering your second DbSet :一个解决方案可能是过滤你的第二个DbSet

 var result =  from a in db.area
               join c in db.sample.Where(s=>s.location_id==a.location_id).OrderByDescending(c => c.sample_date).Take(1)
               on a.location_id equals c.location_id 
               select new 
                    {
                         name = a.location_name,
                         status = c.sample_status,
                         date = c.sample_date
                    };

Another solution could be applying a group join :另一种解决方案可能是应用组加入

 var result =  from a in db.area
               join c in db.sample
               on a.location_id equals c.location_id into samples
               let sample=samples.OrderByDescending(c => c.sample_date).FirstOrDefault()
               select new 
                    {
                          name = a.location_name,
                          status = sample.sample_status,
                          date = sample.sample_date
                    };

If you use navigation properties could be even easier.如果您使用导航属性可能会更容易。 Supposing you have a one to many relationship between Area and Sample :假设您在AreaSample之间有一对多的关系:

var result =from a in db.area
            let sample= a.Samples.OrderByDescending(c => c.sample_date).FirstOrDefault()
            select new 
                    {
                          name = a.location_name,
                          status = sample.sample_status,
                          date = sample.sample_date
                    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq查询以从表中选择数据并联接2个表 - Linq query to select data from table and join 2 tables 如何从 Azure 表存储中查询最近的 n 条记录? - How to query the most recent n records from Azure Table Storage? LINQ to SQL连接两个表以基于子表中的两个不同列两次选择父表 - LINQ to SQL join two tables to select parent table twice based on two different columns from child table LINQ查询最新记录 - LINQ query the most recent records Linq查询以显示两个表中的值,即使其中一个表没有任何对应的记录 - Linq query to display the values from two tables even if one of the table doesn't have any corresponding record Linq 根据最近的总和查询 select 记录 - Linq query to select records based on most recent sum LINQ:从表A中选择第一项,该表在链接到表B的链接表中具有相应的ID - LINQ: Select First Item from Table A that has corresponding ID in linking table that connects to Table B 在具有内部联接的LINQ查询中选择最新日期 - Select the most recent date in a LINQ query with an inner join LINQ从连接表中选择,其中相同的外键具有两个不同ID的记录 - LINQ select from join table where the same foreign key has records for two different IDs LINQ通过联接返回最近日期的记录 - LINQ returning records with most recent dates with a join
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM