[英]Help writing a linq query in subsonic
I'm busy writing some extension methods and so far have been going fine. 我正在忙着写一些扩展方法,到目前为止一切正常。 I have a method getCities that takes in an IQueryable qry and is supposed to return all of the regions in qry that have a region type of city.
我有一个方法getCities,它接受一个IQueryable qry,并且应该返回qry中具有城市区域类型的所有区域。
I tried using linq to do it, but gave up and am currently using a for loop. 我尝试使用linq来做到这一点,但是放弃了,目前正在使用for循环。 It works, but its bugging me.
它可以工作,但是却困扰着我。 I have commented out at the bottom the linq statement i have come up with, that doesn't work.
我在底部注释掉了我想出的linq语句,这是行不通的。
The for loop returns records correctly, the linq query i commented out never returns anything. for循环正确返回记录,我注释掉的linq查询从不返回任何内容。
A region can only have 1 Region_Type, but subsonic makes it a collection, which is why i'm using item.Region_Types.First() to get the regions Region_Type. 一个区域只能有1个Region_Type,但是subsonic使其成为一个集合,这就是为什么我使用item.Region_Types.First()来获取区域Region_Type的原因。
public static IQueryable<Region> getCities(this IQueryable<Region> qry)
{
List<Region> returnCol = new List<Region>();
foreach (var item in qry)
{
if (item.Region_Types.First().Name.ToLower().Trim().Equals("city"))
{
returnCol.Add(item);
}
}
return returnCol.AsQueryable();
//return qry.Where(t => t.Region_Types.First().Name.ToLower().Trim().Equals("city"));
}
Where()返回IEnumerable,而不是IQueryable,您应在语句末尾使用.AsQueryable():
return qry.Where(t => t.Region_Types.First().Name.ToLower().Trim().Equals("city")).AsQueryable();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.