tags:

views:

74

answers:

1

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"));
    }
A: 

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();
mamoo
You are correct, but a method with IQuerable can return an IEnumerable. The problem I have is that the linq statement never returns anything, while my for loop returns the expected records. Sorry if i was unclear.
spaceman
Ok, more clear now :) . Sorry for misunderstanding.Have you tried to replace Equals() with the == operator?
mamoo
yeah, tried .equals, == and compareTo..will see if i can get the querytext subsonic is generating, im beginning to think its a subsonic bug and not a linq problem.
spaceman