21

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
Shai
  • 25,159
  • 9
  • 44
  • 67
  • In this case you can use the [ternary operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator) to refactor it into one assignment, but the question as such remains interesting. – Gert Arnold Dec 24 '21 at 09:31

3 Answers3

44
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. 
Mac
  • 14,615
  • 9
  • 62
  • 80
leppie
  • 115,091
  • 17
  • 196
  • 297
  • Thanks for that, it does help, see my edit for a clarification – Shai Jun 07 '12 at 13:13
  • @Shai: For anonymous types, not easily possible, but can by partially done by a little refactoring of the rest of the method. – leppie Jun 07 '12 at 13:26
  • @Shai: I have added an example of what I meant in my previous comment. – leppie Jun 07 '12 at 13:30
  • 1
    thanks - but that won't compile, as `RestOfMethod` doesn't know which members `u` contains... – Shai Jun 07 '12 at 13:36
  • @Shai: You are correct, and to refactor it more, will be messy :( – leppie Jun 07 '12 at 13:38
  • @Shai: Updated answer, but I think you will agree that it is messy and will be difficult to maintain. – leppie Jun 07 '12 at 13:42
  • thanks for your effort - see the answer I've just posted for this question, I`d be happy to hear some comments – Shai Jun 07 '12 at 13:52
  • @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 – leppie Jun 07 '12 at 14:04
  • The `.Take(0)` seems to translate to SQL best with `Union`. – NetMage May 22 '17 at 23:12
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 => ...));
Shai
  • 25,159
  • 9
  • 44
  • 67
-1

You can declare it as an IQueryable instead of a var. The reason you can't declare fData as a var is because "var" needs to infer the type during declaration. If you know the type ahead of time, you can declare it just fine.

IQueryable fData = null;

Even better, if you know the type of p, you can make it strongly typed with the generic form of IQueryable:

IQueryable<type-of-p> fdata = null;

Note that this assigns null! If you try to use it, you'll get a null reference exception. If you actually want an empty Queryable object, then use leppie's suggestion, and create an empty collection using the Enumerable.Empty().AsQueryable() method chain.

BTownTKD
  • 7,911
  • 2
  • 31
  • 47