简体   繁体   English

无法使用Linq编写Where子句,其中数据包含数组

[英]Unable to write a Where Clause with Linq where data contains an array

I am trying to write a where clause in Linq that matches on a date. 我试图在Linq中写一个匹配日期的where子句。 The values for date is contained in a nested object. date的值包含在嵌套对象中。 What I mean is that the object that contains date has two elements start and finish. 我的意思是,包含日期的对象具有开始和结束两个元素。 I am getting two error messages: 我收到两个错误消息:

  1. Cannot implicity convert System.Collections.Generic.IEnumerable to bool 无法将System.Collections.Generic.IEnumerable隐式转换为bool
  2. Cannot convert lambda expression to delegate type System.Func because some of the return types in the block are not implicity convertible to the delegate return type 无法将lambda表达式转换为委托类型System.Func,因为块中的某些返回类型不能隐式转换为委托返回类型

My code is: 我的代码是:

 var locationName = from relocate in relocations where **relocate.Relocations.
      Where(c=>c.TimeIntervals.Select(d=>d.Start==sh.StartTime.Date))** 
        select relocate.Relocations.Select(a=>a.Path.Items.
             Select(b=>b.DisplayString.Skip(4).SingleOrDefault()));  

It is the bit which is between the double **. 它是在双**之间的位。

Please help!!! 请帮忙!!!

Your relocate.Relocations.Where returns IEnumerable. 您的relocate.Relocations.Where返回IEnumerable。 You need to compare (intersect) that with something, such that result evaluates to a boolean. 您需要将其与某些东西进行比较(相交),以便结果评估为布尔值。

Perhaps like this: 也许像这样:

relocate.Relocations.Where(...).Any()

var locationName = from relocate in relocations where **relocate.Relocations.
      Where(c=>c.TimeIntervals.Select(d=>d.Start==sh.StartTime.Date).Any()).Any()** 
        select relocate.Relocations.Select(a=>a.Path.Items.
             Select(b=>b.DisplayString.Skip(4).SingleOrDefault()));

我认为您只需在语句末尾添加First()FirstOrDefault()

日期和位置之间没有联系-我设法通过使用字典类来解决此问题,然后匹配日期!

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

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