Declaring an empty Queryable?
c#, iqueryable, linq
Solution
IQueryable<type of p> fData = null;
If you want to use the query later (iow after the if):
var fData = Enumerable.Empty<type of p>().AsQueryable();
Update:
Now for using with anonymous types:
IQueryable<T> RestOfMethod<T>(IQueryable<T> rData)
{
var fData = Enumerable.Empty<T>().AsQueryable(); // or = rData;
if(x)
fData = fData.Concat(rData.Where(u => ...));
if(y)
fData = fData.Concat(rData.Where(u => ...));
return fData;
}
// original code location
var rData = some query;
var fData = RestOfMethod(rData);
Update 2:
As pointed out, the above does not actually work, as the predicate of `Where` does not know the type. You could refactor it some more to include the predicates in the parameters, example:
IQueryable<T> RestOfMethod<T>(IQueryable<T> rData,
Expression<Func<T,bool>> pred1,
Expression<Func<T,bool>> pred2)
{ ... }
Update 3: (perhaps hacky)
var fData = rData.Take(0); // should be cheap.
Problem
How do I declare such a variable? ``` var rData = from nc in ctx.NEWSLETTER_CLIENTS join ni in ctx.NEWSLETTER_INDICES on nc.INDEX_NUM equals ni.INDEX_NUM select new { ClientID = nc.CLIENT_ID, Email = nc.CLIENT_EMAIL_ADDRESS, Index = nc.INDEX_NUM, MainClass = ni.MAIN_CLASS, SubClass = ni.SUB_CLASS, App1 = ni.VALUE_1, App2 = ni.VALUE_2, App3 = ni.VALUE_3, App4 = ni.VALUE_4 }; // Now I need to declare on a variable named fData under the function scope, // so I can later use it: var fData = ...; //What do I declare here? if(x) fData = fData.Concat(rData.Where(u => ...)); if(y) fData = fData.Concat(rData.Where(u => ...)); // etc ```