I have this linq query :
var myQuery = from Q in myDataContext
select Q.Name
and when I try to do this : listView.ItemsSource = myQuery
it sometimes throws an exception because there are no elements in myQuery
I tried many ways like : if(myQuery.count!=0)
or if(myQuery.Any())
but nothing worked , so how can I determine if my Query return null ?
It will return an empty enumerable. It won't be null.
LINQ to SQL does not impose C# null or Visual Basic nothing comparison semantics on SQL. Comparison operators are syntactically translated to their SQL equivalents. The semantics reflect SQL semantics as defined by server or connection settings.
Select(q => { var attendanceList = new AttendanceBAL(). GetAttendanceListOf(q. _RollNumber); if (attendanceList == null) return null; return new StudentRecords() { _RollNumber = q. _RollNumber, _Class = q.
if it finds a user, it adds it to the list (not database), and presents a message. if the user does NOT already exist, the program moves on to add the user.
You can realise the result as a list:
var myQuery = (from Q in myDataContext select Q.Name).ToList();
Now you can check the number of items:
if (myQuery.Count > 0) ...
You could also use the Count()
method on the original query, but then you would be running the query twice, once to count the items, and once to use them.
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