Declaring an empty Queryable?
21,819
Solution 1
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.
Solution 2
Well, the following solution might be bad (and even contain some unwanted overhead), but, it works:
var fData = from p in rData
where 0 == 1
select p;
if(x)
fData = fData.Concat(rData.Where(u => ...));
if(y)
fData = fData.Concat(rData.Where(u => ...));
Author by
Shai
Updated on February 07, 2020Comments
-
Shai over 4 years
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
-
leppie almost 12 yearsI highly doubt the will work (or even compile) unless you cast
p
to object. -
BTownTKD almost 12 yearsYou don't need to cast p to object; everything is .NET is implicitly convertible to object.
-
leppie almost 12 yearsIt might work under .NET 4 due to covariance, but it will definitely fail on .NET 3.5.
-
Shai almost 12 yearsThanks for that, it does help, see my edit for a clarification
-
leppie almost 12 years@Shai: For anonymous types, not easily possible, but can by partially done by a little refactoring of the rest of the method.
-
leppie almost 12 years@Shai: I have added an example of what I meant in my previous comment.
-
Shai almost 12 yearsthanks - but that won't compile, as
RestOfMethod
doesn't know which membersu
contains... -
leppie almost 12 years@Shai: You are correct, and to refactor it more, will be messy :(
-
leppie almost 12 years@Shai: Updated answer, but I think you will agree that it is messy and will be difficult to maintain.
-
Shai almost 12 yearsthanks for your effort - see the answer I've just posted for this question, I`d be happy to hear some comments
-
leppie almost 12 years@Shai: It is basically the same as my last update, both will work the same. The
Where
version might be easier on a database. +1 -
NetMage almost 7 yearsThe
.Take(0)
seems to translate to SQL best withUnion
.