LINQ works fine as follows. Query shortened for problem purpose.
var query = (
from p in DBContext.Products
from pp in DBContext.Pricing
select new { p,pp }
)
Now lets say I wanted a list of this query so for example:
var query = List<IQueryable<dynamic>>(); //this is what I assumed the return type would be but no.
for(int = 0; i<5;i )
{
query = (from .... select new {p,pp})
}
So what is correct return type for query to store queries?
CodePudding user response:
Because anonymous types have no public name, it isn't possible to statically declare a variable using an anonymous type directly, but you can use type inference and the var
keyword to have the compiler infer the proper type.
Given some extension methods:
public static class ObjectExt {
public static IQueryable<T> AsIQueryableOfType<T>(this T obj) => default(IQueryable<T>);
public static List<T> AsListOfType<T>(this T obj) => default(List<T>);
}
Then you can declare a IQueryable<T>
or List<T>
for any anonymous type by passing in the anonymous type. Since anonymous type properties can't be declared, but just initialized, you must assign an appropriate expression to the properties, I recommend using default
:
var ListOfQueries = new { p = default(Product), pp = default(Pricing) }
.AsIQueryableOfType()
.AsListOfType();
for(int i = 0; i < 5; i) {
ListOfQueries.Add(from .... select new {p,pp});
}
CodePudding user response:
try this one.
var linqdata = new List<Tuple<DBContext.Products,DBContext.Pricing>>();