Skip to content Skip to sidebar Skip to footer

Convert Iqueryable To

I want to select my price level in database to compare with the an integer number. But It is error : Operator '==' cannot be applied to operands of type 'System.Linq.IQueryable' an

Solution 1:

var list_pricelevel

This is per definition not an int because more than one row can be returned.

I don't use SQL syntax (only lambda) but at the end you want the equivalent of a .FirstOrDefault or Single or First. Basically taking the first row.

Solution 2:

replace:

if (list_pricelevel == 3) 

with:

if (list_pricelevel.First() == 3) 

as you can see here: http://msdn.microsoft.com/en-us/library/bb291976.aspx, if you are sure there is a result or use FirstOrDefault...

Solution 3:

when you have the result from LinQ expression you will always have the list of result set.

So in your code when you are querying as below :

var list_pricelevel = from c in cm.DataContext.Customers where c.WebAccount == userName select c.PriceLevel;

The list_pricelevel will be in the form of List ie IQueryable list,

so you have to get only one element to check with one element

so use the below code :

if (list_pricelevel.Single() == 3) 
{ 
  Response.Write("Welcome");  
}

orif (list_pricelevel.First() == 3) 
{ 
  Response.Write("Welcome");  
}


both the above code gives you only one result setvalue so you can equate with3for validation.

Solution 4:

Here is my suggestion:

if (list_pricelevel.First() == 3) 
 { 
      Response.Write("Welcome");  
 }

This may rise a NullReferenceException in case there is no item in Customers satisfying where c.WebAccount == userName.

Explanation:

list_pricelevel is a IEnumerable of items satisfying your where clause.

Solution 5:

You need get the first item from your collection.

if (list_pricelevel.First() == 3) {
   Response.Write("Welcome");
}

Post a Comment for "Convert Iqueryable To "