Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate empty IQueryable for use with Linq to sql

I need to be able to either have an optional parameter in a Linq query, or be able to assign the query to a var in something like an IF if that optional parameter needs to be removed from the query.

If I set the query var inside the IF statement then it tells me that the var doesn't exist in context when I try to loop through it.

if (whichGroup == "All")         {             var listOppLineData = (from o in db.opportunity_vws                                    where o.fiscal_yr_and_qtr == qtr                                    select o                                   );         }         else         {             var listOppLineData = (from o in db.opportunity_vws                                    where o.fiscal_yr_and_qtr == qtr && o.Group == whichGroup                                    select o                                   );         }          foreach (var data in listOppLineData)  //listOppLineData doesn't exist here         { 

I need to set the var before the IF statement I think, but I don't know what to set it to.

var listOppLineData = ""; // gives me  Cannot implicitly convert type 'System.Linq.IQueryable<Forecast.Models.opportunity_vw>' to 'string'  IQueryable listOppLineData = new IQueryable(); //gives me         Cannot create an instance of the abstract class or interface 'System.Linq.IQueryable' 
like image 752
user2073077 Avatar asked Feb 21 '13 01:02

user2073077


People also ask

How do you declare IQueryable?

Answers. Enumerable. Empty<T>(). AsQueryable();

When should I use IQueryable and IEnumerable using LINQ?

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation.

What is IQueryable in LINQ?

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed. The definition of "executing an expression tree" is specific to a query provider.

How do I combine two Iqueryables?

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.


2 Answers

Try this. You can create a generic type with T or a specific type by replacing T with your type name.

IQueryable listOppLineData = Enumerable.Empty<T>().AsQueryable() 
like image 135
Krishna Avatar answered Sep 18 '22 19:09

Krishna


Try this:

IQueryable<opportunity_vw> listOppLineData; 

This is defining the variable, but you are waiting to initialise it.

Also, looking at your query, you could do it all in one, like so:

var listOppLineData = (from o in db.opportunity_vws                       where o.fiscal_yr_and_qtr == qtr && (o.Group == whichGroup || whichGroup == "All")                       select o                       ); 
like image 42
Simon C Avatar answered Sep 21 '22 19:09

Simon C