LINQ OrderBy versus ThenBy

You should definitely use ThenBy rather than multiple OrderBy calls. I would suggest this: tmp = invoices.InvoiceCollection .OrderBy(o => o.InvoiceOwner.LastName) .ThenBy(o => o.InvoiceOwner.FirstName) .ThenBy(o => o.InvoiceID); Note how you can use the same name each time. This is also equivalent to: tmp = from o in invoices.InvoiceCollection orderby o.InvoiceOwner.LastName, o.InvoiceOwner.FirstName, o.InvoiceID select o; If you … Read more

System.LINQ.Dynamic: Select(” new (…)”) into a List (or any other enumerable collection of )

First, you’ll access the current grouped value as Key in your Select clause: .Select(“new (Key as Group, Sum(Value) as TotalValue)”); That should make your query work. The harder question is how to turn the returned objects, which will have a dynamically generated type that inherits from DynamicClass, into a static type. Option 1: Use reflection … Read more

LINQ across multiple databases

You can do this, even across servers, as long as you can access one database from the other. That is, if it’s possible to write a SQL statement against ServerA.DatabaseA that accesses ServerB.DatabaseB.schema.TableWhatever, then you can do the same thing in LINQ. To do it, you’ll need to edit the .dbml file by hand. You … Read more

LINQ recursion function?

LINQ doesn’t really “do” recursion nicely. Your solution seems appropriate – although I’m not sure HasChildren is really required… why not just use an empty list for an item with no children? An alternative is to write a DescendantsAndSelf method which will return all of the descendants (including the item itself), something like this; // … Read more

How to view LINQ Generated SQL statements?

You can always attach something to the .Log property of your DataContext. That will show all the SQL commands as they are sent. I do this in my base for data access objects and output it to the Visual Studio debug console. As the objects create their DataContext I check it see if its debug … Read more

How to view LINQ Generated SQL statements?

You can always attach something to the .Log property of your DataContext. That will show all the SQL commands as they are sent. I do this in my base for data access objects and output it to the Visual Studio debug console. As the objects create their DataContext I check it see if its debug … Read more

LINQ Between Operator

If you express it as a where clause it may just work out of the box with LINQ to SQL, if you can construct an appropriate expression. There may be a better way of doing this in terms of the expression trees – Marc Gravell may well be able to improve it – but it’s … Read more

LINQ Partition List into Lists of 8 members [duplicate]

Use the following extension method to break the input into subsets public static class IEnumerableExtensions { public static IEnumerable<List<T>> InSetsOf<T>(this IEnumerable<T> source, int max) { List<T> toReturn = new List<T>(max); foreach(var item in source) { toReturn.Add(item); if (toReturn.Count == max) { yield return toReturn; toReturn = new List<T>(max); } } if (toReturn.Any()) { yield return … Read more