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
The simple answer is that unless you add the record to the underlying datastore that the Iqueryable is querying, you can't add a new record into an IQueryable. So if you are using LinqToSql then you would have to add a row into the table that the IQueryable was querying in order to "add" a row into the IQueryable.
var list4 = list1. Union(list2); Union is a set operation - it returns distinct values. Concat simply returns the items from the first sequence followed by the items from the second sequence; the resulting sequence can include duplicate items.
Answers. Enumerable. Empty<T>(). AsQueryable();
IQueryable<T> is a C# interface that lets you query different data sources. The type T specifies the type of the data source that you're querying. Under the hood, IQueryable uses expression trees that translate LINQ queries into the query language for the data provided.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With