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.IQue
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");
}
or
if (list_pricelevel.First() == 3)
{
Response.Write("Welcome");
}
both the above code gives you only one result set value so you can equate with 3 for validation.
That's the beauty of Linq that every query returns an IQueryable so you can defer getting the final result until you really decided what you want. On the other word you can execute a query over another query :) So in order to get the real data out of a query you should execute a command over it that actually returns what you want. In your case since you are expecting your query to return only one value any methods like "FirstOrDefault","Single" or "First" will do the trick
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.
You need get the first item from your collection.
if (list_pricelevel.First() == 3) {
Response.Write("Welcome");
}
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
...
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.