I have the following Linq to SQL query, in which I’m trying to do a multi-column GROUP BY:
return from revision in dataContext.Revisions where revision.BranchID == sourceBranch.BranchID-1 && !revision.HasBeenMerged group revision by new Task(revision.TaskSourceCode.ToUpper(), revision.TaskSourceID) into grouping orderby grouping.Key ascending select (ITask)grouping.Key;
This throws InvalidOperationException (‘Cannot order by type ‘Task’.’).
Is there an interface that Task must implement (it already implements IComparable, IComparable<ITask>)? Does Task need to be a Linq to SQL Entity (it isn’t, currently, since there’s no corresponding table). Or is this just something that Linq to SQL doesn’t support?
Note that I’ve already tried an anonymous type for the grouping, which failed with a similar InvalidOperationException:
... group revision by new { Code = revision.TaskSourceCode.ToUpper(), Id = revision.TaskSourceID } ...
For what it’s worth, note that the Task object is a composite of 2 fields; one is a single character (typically ‘S’ or ‘B’) and the other is an int (actually a cross-database ‘foreign key’ if you like). The act of ordering by Tasks just compares their string representations; E.G. Task ‘B101’ < Task ‘S999’
Looks like you already have your solution, but just FYI LINQ to SQL does support
.ToUpper().For instance:
Is translated into:
Hope that helps.