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 => ...));
Share:
21,819
Shai
Author by

Shai

Updated on February 07, 2020

Comments

  • Shai
    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
    leppie almost 12 years
    I highly doubt the will work (or even compile) unless you cast p to object.
  • BTownTKD
    BTownTKD almost 12 years
    You don't need to cast p to object; everything is .NET is implicitly convertible to object.
  • leppie
    leppie almost 12 years
    It might work under .NET 4 due to covariance, but it will definitely fail on .NET 3.5.
  • Shai
    Shai almost 12 years
    Thanks for that, it does help, see my edit for a clarification
  • leppie
    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
    leppie almost 12 years
    @Shai: I have added an example of what I meant in my previous comment.
  • Shai
    Shai almost 12 years
    thanks - but that won't compile, as RestOfMethod doesn't know which members u contains...
  • leppie
    leppie almost 12 years
    @Shai: You are correct, and to refactor it more, will be messy :(
  • leppie
    leppie almost 12 years
    @Shai: Updated answer, but I think you will agree that it is messy and will be difficult to maintain.
  • Shai
    Shai almost 12 years
    thanks for your effort - see the answer I've just posted for this question, I`d be happy to hear some comments
  • leppie
    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
    NetMage almost 7 years
    The .Take(0) seems to translate to SQL best with Union.