C# 无法使用Linq Where数据包含数组写入Where子句
我试图在Linq中编写一个where子句,该子句与日期匹配。日期的值包含在嵌套对象中。我的意思是,包含date的对象有两个元素start和finish。我收到两条错误消息:C# 无法使用Linq Where数据包含数组写入Where子句,c#,linq,C#,Linq,我试图在Linq中编写一个where子句,该子句与日期匹配。日期的值包含在嵌套对象中。我的意思是,包含date的对象有两个元素start和finish。我收到两条错误消息: 无法将System.Collections.Generic.IEnumerable隐式转换为bool 无法将lambda表达式转换为委托类型System.Func,因为块中的某些返回类型不能隐式转换为委托返回类型 我的代码是: var locationName = from relocate in relocations
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()));
这是双**之间的位
请帮忙 您的
重新定位。重新定位。其中
返回IEnumerable。您需要将其与某个对象进行比较(相交),以便结果的计算结果为布尔值
也许是这样的:
relocate.Relocations.Where(…).Any()
我认为您只需在语句末尾添加一个
First()
或FirstOrDefault()
。日期和位置之间存在脱节-我已通过使用dictionary类并在日期上进行匹配来解决此问题 var locationName听起来像是字符串或其他东西。但是您的查询返回一个IEnumerable
。这是对的吗?是的,我试图获取一个字符串值的名称。它本身保存在包含5个值的数组对象中。将Any()添加到末尾并不能解决问题,它仍然显示上面给出的相同错误消息。Any()应该处理错误1。错误2可能是剩下的。将Any()添加到日期时间间隔的条件中。Any(d=>d.Start==sh.StartTime.Date))。Any()工作但返回多个结果,并且仍然是IEnumerables,我在回答中说,relocate.Relocations.Where()
返回IEnumerable,但relocate.Relocations.Where().Any()返回布尔值。我认为你需要注意你的错误。第一。我稍微编辑了我的答案,在嵌套的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()));