簡體   English   中英

使用Join在LINQ中實現where子句

[英]Implementing where clause in LINQ with join

我有這個LINQ(LINQ TO ENTITY):

var clientFullReview = (from cr in clientReview
                        join ir in inspectionReviews on cr.siteId equals ir.SiteId into g
                        from subsite in g.DefaultIfEmpty()
                        select new
                        {
                         clientId = cr.clientId,
                         clientName = cr.clientName,
                         siteId = cr.siteId == null ? -1 : cr.siteId,
                         inspectionReviewId = (subsite == null ? -1 : subsite.Id),
                         inspectionFrequency = (subsite == null ? -1 : subsite.FrequencyId),

                          isNormal = (subsite == null ? false : subsite.IsNormal)
                         });

在上面的鏈接中,我需要使用where子句。

inspectionReviews-具有日期屬性。 在LINQ上方,我只想加入具有特定日期的inspectionReviews記錄,例如:

    var clientFullReview = (from cr in clientReview
                            join ir in inspectionReviews on cr.siteId equals
                            where ir.DateReview.Year == date.Year &&
                            ir.DateReview.Month == date.Month 
                            into g
                            from subsite in g.DefaultIfEmpty()
                            select new
                            {
                             clientId = cr.clientId,
                             clientName = cr.clientName,
                             siteId = cr.siteId == null ? -1 : cr.siteId,
                             inspectionReviewId = (subsite == null ? -1 : subsite.Id),
                             inspectionFrequency = (subsite == null ? -1 : subsite.FrequencyId),

                              isNormal = (subsite == null ? false : subsite.IsNormal)
                             });

但是,當我嘗試使用where子句實現它時,出現此錯誤:

A query body must end with a select clause or a group clause

關於這個關鍵詞: into第二個LINQ。

所以我的問題是在實現連接時如何按日期過濾數據?

單程

join ir in inspectionReviews.Where(x =>
    x.DateReview.Year == date.Year && x.DateReview.Month == date.Month)
on cr.siteId equals ir.SiteId

其他方式

join ir in (from x in inspectionReviews
    where x.DateReview.Year == date.Year && x.DateReview.Month == date.Month
    select x)
on cr.siteId equals ir.SiteId

另一種方式

join ir on cr.siteId equals ir.SiteId into g
from subsite in g
    .Where(x => x.DateReview.Year == date.Year && x.DateReview.Month == date.Month)
    .DefaultIfEmpty()

Join子句只允許等於,因此,如果需要過濾聯接的集合,則可以在第二個from子句下使用subsite變量:

join ir in inspectionReviews on cr.siteId equals ir.SiteId
into g
from subsite in g.DefaultIfEmpty()
where subsite.DateReview.Year == date.Year &&
subsite.DateReview.Month == date.Month 

將其分為兩個查詢,然后從最篩選的第一個列表中進行第二個選擇。

我不會重現您的代碼,因為我認為第二個聯接值上缺少一些文本,但是我們的想法是分兩個步驟完成:

 var clientFullReviews = (from cr in clientReview
                        join ir in inspectionReviews on cr.siteId equals
                        where ir.DateReview.Year == date.Year &&
                        ir.DateReview.Month == date.Month 
                        into g

 var clientcurrent reviews =(from cr clientFullReviews select new
                        {
                         clientId = cr.clientId,
                         clientName = cr.clientName,
                         siteId = cr.siteId == null ? -1 : cr.siteId,
                         inspectionReviewId = (subsite == null ? -1 : subsite.Id),
                         inspectionFrequency = (subsite == null ? -1 : subsite.FrequencyId),

                          isNormal = (subsite == null ? false : subsite.IsNormal)
                         });

這不是完美的語法,因為我不太了解您的數據對象,但是您明白了。 我不確定您是否會因此受到性能的影響,但是我幾乎總是這樣將其拆開,以保持Linq語法的整潔和可讀性(並避免在一行中將太多擴展表達式與自己混淆!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM