[英]Implementing where clause in LINQ with join
I have this LINQ (LINQ TO ENTITY): 我有这个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)
});
In the link above I need to use where clause. 在上面的链接中,我需要使用where子句。
inspectionReviews - has date property. inspectionReviews-具有日期属性。 In the LINQ abouve I want to make join only inspectionReviews records that has specific date ,like that:
在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)
});
But when I try to implement it with where clause I get this error: 但是,当我尝试使用where子句实现它时,出现此错误:
A query body must end with a select clause or a group clause
on this key word: into
in second LINQ. 关于这个关键词:
into
第二个LINQ。
So my question how can I filter data by date when I implement join? 所以我的问题是在实现连接时如何按日期过滤数据?
One way 单程
join ir in inspectionReviews.Where(x =>
x.DateReview.Year == date.Year && x.DateReview.Month == date.Month)
on cr.siteId equals ir.SiteId
Another way 其他方式
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
Yet another way 另一种方式
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 clause allows only equals, so if you need to filter the joined collection, you can use the subsite variable under the second from clause: 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
Break it intotwo queries and do your second select from the filterest first list. 将其分为两个查询,然后从最筛选的第一个列表中进行第二个选择。
I am not going to reproduce your code because I think it has some text missing, on the second join value, but the idea is to do it in two steps: 我不会重现您的代码,因为我认为第二个联接值上缺少一些文本,但是我们的想法是分两个步骤完成:
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)
});
This isn't perfect syntax because I don't know your data objects well enough, but you get the idea. 这不是完美的语法,因为我不太了解您的数据对象,但是您明白了。 I am not sure if you will take a performance hit doing it this way, but I almost always break it up like this to keep my Linq syntax clean and readable (and to keep from confusing myself with too many extension expressions in one line!)
我不确定您是否会因此受到性能的影响,但是我几乎总是这样将其拆开,以保持Linq语法的整洁和可读性(并避免在一行中将太多扩展表达式与自己混淆!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.