Tag

LINQ Archives - Falafel Software Blog

You can use LINQ’s ‘let’ keyword to tune the emitted SQL

By | .NET, C# | 10 Comments

Here’s an interesting little factoid I just stumbled across during ordinary development. I wanted to see if projecting a row into an object with a nested object would impact the generated SQL. Here’s the original query:

And here’s what I wanted to test to see if the resulting SQL would be any different:

And it wasn’t. Here’s the output SQL for both LINQ queries above. (Anyone else bothered by the term “LINQ query?” Because if you expand the acronym, you’re saying “Language Integrated Query query.”)

So now I know that I can group together related columns in an…

Read More

LINQ Aggregate Operator: You Don’t Need It Until You Need It!

By | .NET | No Comments

Lately I write so many LINQ queries on an almost daily basis that it is easy to think I have used every kind of operator in one way or another. It’s kind of fun, really – trying to come up with the most efficient query to get what I want out of a collection of items, for example.  But even when I have been using it for a while, LINQ can still surprise me! I have used the other aggregation operators repeatedly, and they are perfect for my typical aggregation needs:  sum, count, average, min, and max.  But this Aggregate…

Read More

The Four Deadly Sins of LINQ Data Access: Part 3–Insufficiently Chatty Queries

By | News | No Comments

This is post 3 of 4 in the series “The Four Deadly Sins of LINQ” Last time in this series, I showed how a common and easy-to-use method of Entity Framework would result in more columns than necessary being returned from the database, and before that, I described the cause and solution for Overly Chatty Queries. At the end of the last post, I said that yes, there is such a thing as an “Underly” chatty query, and I suspect that statement might have sounded odd to some. After all, the first post was all about how bad it is…

Read More

Underscore: LINQ (almost) for JavaScript

By | Uncategorized | No Comments

As part of my emerging series on LINQ from Scratch, I’d like to take a small detour today into what you do if you need the functionality of LINQ in your JavaScript program.   One answer is the underscore.js library.  Here’s what they say about themselves: Underscore provides 80-odd functions that support both the usual functional suspects: map, select, invoke — as well as more specialized helpers: function binding, JavaScript templating, deep equality testing, and so on. It delegates to built-in functions, if present, so modern browsers will use the native implementations of forEach, map, reduce, filter, every, some and indexOf….

Read More

Using Sitefinity’s Dynamic LINQ to filter classifications

By | Sitefinity | No Comments

What is Dynamic Linq anyway? Dynamic LINQ has been around in the .NET space for a while. To know more about what Dynamic Linq is and how to use it in general (out of Sitefinity) please head to Scott Gu’s post. Now lets learn about Dynamic Linq in Sitefinity’s context. Sitefinity has implemented its own version of Dyanmic Linq under Telerik.Sitefinity.Data.Linq.Dynamic namespace. In this post we will see how you, the reader, can you Sitefinity’s Dynamic Linq and filter List Items based on classification. Why would I need Dynamic Linq? Dynamic Linq as Scott Hanselman describes makes custom Linq expressions…

Read More

The Four Deadly Sins of LINQ Data Access: Part 1–Overly Chatty Queries

By | News | No Comments

This is post 1 of 4 in the series “The Four Deadly Sins of LINQ” Introduction I love using LINQ to filter, transform, and aggregate collections. LINQ provides a very concise and expressive syntax that lets you do more with fewer lines of code, which in turn means generally higher productivity, reduced maintenance, and fewer bugs. However, all of these things are unconditionally true only regarding in-memory collections. When it comes to querying a relational DB, LINQ has a dark side: it’s almost too good at hiding what’s really happening. This could cause a naïve developer to write poor data…

Read More

LINQ From Scratch #2–Deferred Execution

By | Uncategorized | No Comments

A key, but often overlooked aspect of LINQ is that execution of query statements is deferred until you actually ask for the first item in the sequence. Consider this code, var list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var query = from num in list where num < 7 select num; // other code here foreach ( var num in query ) { Console.WriteLine(num); } C# applications typically execute from top to bottom.  You might, therefore, reasonably expect that the query would run right after the declaration of the list.  It…

Read More

Debugging in LINQ Hell

By | Uncategorized | One Comment

As consultants, we are often called upon to maintain or even rescue large, existing projects with poorly written code. When we are called in at the last minute, time is of the utmost essence and there is no time for the luxury of rewriting or even refactoring. One particularly nasty problem that can come of this is when our services are needed to debug something that has gone wrong in a method that is hundreds of lines long, composed of LINQ statement built upon LINQ statement ad infinitum. The complication here comes the from Visual Studio debugger’s very poor support…

Read More

LINQ From Scratch

By | Uncategorized | No Comments

This is part one of a series of blog posts on LINQ.  I’m writing it because I need to remind myself how to use LINQ well, and because, while LINQ was covered extensively when it was first introduced, it has been a bit neglected lately. LinqPad Step 1 in learning LINQ is to get the right programming editor.  I can make a case for Visual Studio, for Sublime, for many editors, but for learning LINQ there really is only one good choice: LinqPad.  This brilliant tool is great for learning LINQ and for working in LINQ and is worth its…

Read More

Calculated Field Queries with LINQ to Entities

By | C# | No Comments

Entity Framework can save you a lot of time when developing data operations in your application.  I am going to demonstrate some techniques to use calculated fields to filter or constrain your results in a LINQ to Entity query. Before we get started, we’ll use a simple example of a Contact entity that has one or more Address entities associated with it: public class Contact { public int ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime Birthday { get; set; } public List<Address> Addresses { get; set;…

Read More

Dynamically Retrieving Sitefinity Form Entries

By | Sitefinity | No Comments

You can access Sitefinity Form entries using GetValue(), but you may want to chug through new form columns without knowing what they are named. In the blog How to access Sitefinity Form Response Data Programmatically, Gary Campbell explores syntax that looks something like this: var temp = formEntry.GetValue(“MyFormColumnName”); You need to know “MyFormColumnName” to get the form entry value. Telerik hasn’t surfaced methods to snag these easily, but using LINQ we can spelunk into a form’s description and rip out what we want. Get All Form Columns To get all form columns including the built-in columns like “User Id”, “Submitted…

Read More

Grouping Complex Objects for LongListSelector

By | Mobile | No Comments

The previous blog on Categorized Lists with LongListSelector demonstrated IGrouping implementation. If we play it right, we shouldn’t have to touch the “Grouping” class to group using complex objects. I’ll use a playing card deck as a familiar data model that has categories to group on, i.e. the “Suit” that contains “Clubs”, “Spades”,  “Hearts” and “Diamonds” and where each card has a value from two through nine and including “Jack”, “Queen”, “King” and “Ace”. The cards can appear in the LongListSelector with the Suit in the heading for each group and the value for each card making up the details…

Read More

Categorized Lists with Windows Phone 7 LongListSelector

By | Mobile | No Comments

You can use LongListSelector as a glorified ListBox, but that cripples its true purpose. The real power  of LongListSelector is to display long categorized lists, enable a “People Hub” style “jump list” to navigate between groups in the list and to allow performant scrolling. Grouping The trick to get LongListSelector working is to properly group the data assigned to the LongListSelector ItemsSource. You can start with a simple flat list of objects, where each object has a property suitable for grouping. In the snippet below, “MyObject” has a “Category” property that the list will be grouped on. public class MyObject…

Read More

Adding Items to a List in Sorted Order

By | C# | No Comments

The Problem I wanted to be able to add items to a IList<T> but not at the beginning or end of the list—in order based on a sorting function I specified.  I could have used List<T>’s Sort() method that takes an IComparer<T> as a parameter, but then I would have to define a new comparer class for every class I wanted to store in the list (not to mention every time I wanted to sort one those types off of a different property).  The Solution Instead of this potentially bulky solution, I created a generic extension method that will add…

Read More