简体   繁体   中英

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. The values for date is contained in a nested object. 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
  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

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. 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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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