i have a problem with linq, i have the following code:
Models.Users user = null;
foreach (var item in db.Users)
{
if (item.username.Equals(username))
{
ausgabe = true;
user = item;
}
}
// linq, does not work
user = null;
var test = from u in db.Users
where u.username.Equals(username)
select u;
user = (Models.Users)test;
at the runtime i get a error because test i IQueryable, but how to make this conversation?
It looks like the problem is casting a query to a user, when you want to execute the query, so probably:
(see also
FirstOrDefault(),First(), andSingle())Also, for convenience and readability, can I suggest:
(which also, in regular code, avoids the problem of
u.usernamebeingnull, and thus causing aNullReferenceException)