for example,
var result = from category in myEntities.Categories
where category.UserId == userId
orderby category.CategoryName
select category;
What is the type of “result”? I casted it to an IQueryable variable and it worked except it did not have some of the methods normally available, e.g. Count(). In my program I have to cast it to some explicit types and then use the Count() method. What should I do?
Use this:
I don’t see why count would be unavailable in this scenario. Maybe if you were casting to plain
IQueryableand notIQueryable<Category>?EDIT: Just tested it. It’s definitely because you used a generic IQueryable without a type. .Count() is not available when you use that. You must include the type.
EDIT2: If you are creating an anonymous type then you have to use the var keyword. You will need to create a type in order to cast it.
Create a custom type like this:
Then instantiate your enumerable of custom types like so:
You cannot use IQueryable when doing this as IQueryable allows you to send expression trees to the datasource. myEntities.Categories implements IQueryable so the query we just listed is executed at the source, but further queries on your
IEnumerable<MyCustomType>Will be executed on the objects in memory.