简体   繁体   中英

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.

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

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

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