开发者

Help writing a linq query in subsonic

开发者 https://www.devze.com 2022-12-27 05:01 出处:网络
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 reg

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.

I tried using linq to do it, but gave up and am currently using a for loop. 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.

The for loop returns records correctly, the linq query i commented out never returns anything.

A region can only h开发者_运维知识库ave 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.

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() returns an IEnumerable, not an IQueryable, you should use .AsQueryable() at the end of the statement:

return qry.Where(t => t.Region_Types.First().Name.ToLower().Trim().Equals("city")).AsQueryable();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号