简体   繁体   English

LINQ-多个左外部联接仅用于最新记录

[英]LINQ - multiple left outer joins for most recent records only

I have a sample sites table with 3 child tables which can have multiple records for each sample site. 我有一个带有3个子表的示例站点表,每个子站点可以有多个记录。 I needed to do a left outer join so I would still get all the sample sites, even if they don't all have associated child records (one of the tables is empty right now). 我需要做一个左外部联接,所以即使它们没有关联的子记录(其中一个表现在为空),我仍将获得所有示例站点。 Based on some research I did on SO, I did this: 根据我对SO所做的一些研究,我做到了:

SampleSiteDataViewModel viewModel = 
            (from s in db.SAMPLESITES_EVW
            from b in db.BACTERIA_EVW.Where(b => b.FIELDSITEID == fieldSiteId).DefaultIfEmpty()
            from c in db.CHEMISTRY_EVW.Where(c => c.FIELDSITEID == fieldSiteId).DefaultIfEmpty()
            from m in db.MACROS_EVW.Where(m => m.FIELDSITEID == fieldSiteId).DefaultIfEmpty()
            where s.FIELDSITEID == fieldSiteId
            select new SampleSiteDataViewModel
                {
                    MPN_ECOLI = b.MPN_ECOLI,
                    DO_PERCENT = c.DO_PERCENT,
                    PH = c.PH,
                    TDS = c.TDS,
                    TEMP_C = c.TEMP_C,
                    OE = m.OE,
                    DESCRIP = s.DESCRIP
                }).FirstOrDefault();

However, I realized that this won't necessarily return the most recent bacteria/chemistry/macro record if there are multiple records, since it isn't a 1 to 1 relationship. 但是,我意识到,如果有多个记录,这不一定会返回最新的细菌/化学/宏观记录,因为它不是一对一的关系。 So, I tried replacing .DefaultIfEmpty with .OrderByDescending(b => b.SAMPLEDATETIME).FirstOrDefault() , but I got this error: 因此,我尝试将.DefaultIfEmpty替换为.OrderByDescending(b => b.SAMPLEDATETIME).FirstOrDefault() ,但出现此错误:

"An expression of type 'BACTERIA_EVW' is not allowed in a subsequent from clause in a query expression with source type 'DbSet'. Type inference failed in the call to 'SelectMany'" “在源类型为'DbSet'的查询表达式的后续from子句中,不允许类型'BACTERIA_EVW'的表达式。在对'SelectMany'的调用中类型推断失败

How can I correctly select only the most recent records while still accounting for situations where no records exist? 在仍然考虑不存在任何记录的情况下,如何仅正确选择最新记录? I think it would be simpler to break it down into multiple linq queries but I'd rather do it in a single database call if possible. 我认为将其分解为多个linq查询会更简单,但如果可能,我宁愿在单个数据库调用中进行。

ETA - this is using an ESRI Geodatabase loaded in SQL Server using an Entity Framework "code first from database" model, which (I believe) doesn't allow for navigation properties using that method. ETA-使用的是使用实体框架“数据库优先编码”模型在SQL Server中加载的ESRI地理数据库,(我认为)该模型不允许使用该方法的导航属性。

You are almost there. 你快到了。

Replacing 更换

.DefaultIfEmpty()

calls with 打电话给

.OrderByDescending(b => b.SAMPLEDATETIME).FirstOrDefault()

is fine. 很好 But it changes the result type from sequence to single item, so you also need to replace the corresponding from keywords with let keywords and you are done. 但是它将结果类型从序列更改为单个项目,因此您还需要用let关键字替换from关键字中的相应内容from然后就可以完成。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM