Search code examples
c#linqlinq-to-sqlwebmethod

What does the variable hold if there is no match after running a LINQ


[WebMethod]
public List<FavoritesTO> getFavorites(string username)
{
    using (FavoritesDataContext db = new FavoritesDataContext ())
    {
        var query = from row in db.Favorites
                    where username == row.username
                    select row.imdbId;
        // here
    }
}

In the area marked as here, what will the query variable hold, say in case there is no username in my table which matches with the username passed to the function?


Solution

  • The value of query will just be the query - until you try to evaluate the results, nothing knows whether or not there are any. After that, I suspect that LINQ to SQL will cache the results - but it's still conceptually just a query.

    If you're asking whether it's null or not, no - it's just an empty sequence. So query.Count() would return 0, query.Any() would return false etc.