Random thoughts of wisdom - the personal blog of Michael Holm Andersen

C# and LINQ. Extension methods overview

Found an awesome article (serie) about C# – part 3 discussed C# and LINQ and contains a detailed description of the different extension methods used in LINQ – a “must read” article:

Clean copy of LinqToSQL Entity object

Often you need to make a copy of a LinqToSQL (Entity) Object with all the values intact. For some reason the .NET framework does not have a method for this. However the below code will make a “clean copy” of a LinqToSQL object. // Instanciate new object and copy all properties from old Bi... [More]

LINQ to SQL – Group 2 rows into 1

Jacob and I have been struggling with some “nasty” LINQ today where we need to get the aggregated value of a Quantity column from a transaction table, however a little “twist” was that we need to sum all the positive values into one group and all the negative values into another, and in the result s... [More]

AutoComplete, PageMethod and LINQtoSQL

I’m using AutoComplete to enhance the functionality of a TextBox and make it act like the below image, where the user enters the customer accountnumber, which is then displayed together with their name (the blurry stuff in the picture :-) As I have to use this function... [More]

LINQtoSQL – Grouping and selecting

I have a Order->OrderLine table setup where I need to check if any “duplicate” orders exists. In order to check this I need to be able to select select various columns *not* contained in the group (key) – playing around in LinqPad I quickly came up with this query:from line in AxSaleLines where ... [More]

LINQ and PagedList

If you’re using LINQ you probably know about the .Skip and .Take methods. However often you need to do paging, hence you probably use something similar to this to get around that: public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)    {   ... [More]

Using LINQPad to query the database

If you’re working with LINQ, you simply can’t live without LINQPad. Below is a screenshot from the query:from ans in UserAnswers group ans by new {ans.AnswerId, ans.Answer.Content, Q=ans.Answer.Question.Content} into myGroup select new {myGroup.Key.Q, myGroup.Key.Content, Antal ... [More]

LINQ Projection and SelectListItem

A small example of where LINQ Projection might come in handy is when you’re working with MVC. In the below example LINQ Projection is used to transform a enumeration of EmployeeCategory instances into an enumeration of SelectListItem instances: var rep = new mhaRepository(); var person = rep.GetP... [More]

InvalidCastException on linq DB submit with non-integer key – aka LinqToSQL bug

Wasted a whole day on a (confirmed) bug in LinqToSQL. The problem is this: “When database contains two tables, both with automatic integer primary keys, and a relationship between a unique char field in one and a non-unique char field in the other, inserting new rows into the second table fails on ... [More]

“Dynamic” LINQ queries – using query composition and deferred execution

LINQtoSQL is GREAT, however we often need to perform dynamic queries, which is not directly supported by LINQ – however if we use the capability of LINQ to do query composition and deferred execution we can perform most of the ‘dynamic’ queries we need (without having to use the PredicateBuilder). ... [More]