If you have a list, array, or query in a C#/LINQ application and need to check and see if the list is empty, the correct way to do this is to use the Any() extension method:
if (q.Any())
{
Similarly, you can check to see if any elements in the list meet a certain condition:
if (q.Any(i => i.IsSpecial))
{
If the query provider is something like LINQ to Entities, this will be translated into fairly efficient SQL using EXISTS.
For some reason, I see a lot of people write this co...
In this post I will demonstrate that use of the join keyword in LINQ to SQL and LINQ to Entities is nearly always wrong. LINQ queries which you write with the join keyword are harder to read and write than queries you write using associations, and they require knowledge of database metadata which is not required otherwise. This introduces the potential for errors and makes maintenance harder.
Many people ask how to do a "left join" in LINQ to SQL, and unfortunately, the answer they nearly alw...
In this post, I will demonstrate how to map entity models to views in an ASP.NET MVC application without worrying about implementation details like eager loading, lazy loading, or having to manually optimize SQL for the task at hand. I will argue that expressing the relationship between an entity model in the presentation model in a LINQ projection is far simpler than other methods of doing this mapping.
Imagine that you've been asked to write a new web application to track employees for a cu...
A couple of weeks ago, I wrote In LINQ, Beware of Skip(0). In that post, I observed that calling Skip(0) on a query result in LINQ, which has no effect on the dataset returned, imposes a performance penalty with at least some LINQ providers. At the time I commented that there might be some desirable behavior of this that I had missed. Sure enough, one of the developers on the LINQ to SQL team noted in comments that Skip(0) will cease to be a no-op in LINQ to SQL in .NET 4.0, and supplied a perfe...
Calling IQueryable<T>.Skip(0) seems like it should be "free." In other words, since it will have no effect on the resulting data, there should be little to no performance cost for calling it. But this is demonstrably not true in LINQ to Entities, and it occurs to me that LINQ providers are not required to optimize it away. Therefore, it is probably a good idea to avoid making such a call at all, so that you do not have to concern yourself with whether the provider will generate a slower qu...
If you spend enough time with the PagedList class that I've been using for paging in the method which supplies data to jqGrid, it's a near-certainty that sooner or later you will see a LINQ error with Count in the call stack. The error may seem confusing, because it has nothing to do with Count. Commentor Graeme has been experimenting with my demo solution, and has run into just this issue:
Finding your blogs very helpful - superbly written - just found out about jqgrid last night and have inte...
LINQ is getting hotter and hotter. Just announced on the PDC, and now Danny is blogging about it.
Language Integrated Query seems to be the biggest feature of upcoming C# 3.0. Searching for LINQ resources I have found The LINQ Project whitepaper written by Don Box (that used to love COM) and Anders Hejlsberg (The Architect). This is definitely not for beginners. There are some cool features like type inference. Eventhough the C# language remains strongly typed, a programmer can define a vari...