2

I think I've read every post on this subject but still can't seem to get this to work. I want to duplicate the following GroupBy with dynamic Linq. I've tried this Dynamic GroupBy but can't seem to get the First().

var result = mydata
    .GroupBy(g => g.ID)
    .Select(z => z.First())
    .Select("new (ID, Field1, Field2)");

The GroupBy needs to be dynamic. I need the first row of the grouped field. So..

ID:1, Field1:W, Field2:L 
ID:1, Field1:A, Field2:B 
ID:2, Field1:A, Field2:B 
ID:2, Field1:C, Field2:D 

Should end up being:

ID:1, Field1:W, Field2:L 
ID:2, Field1:A, Field2:B 
OR
ID:1, Field1:A, Field2:B 
ID:2, Field1:C, Field2:D 
Community
  • 1
  • 1
user3478586
  • 307
  • 4
  • 15

2 Answers2

4

I solved it by adding to DynamicQueryable

public static object First(this IQueryable source)
        {
            if (source == null) throw new ArgumentNullException("source");
            return source.Provider.Execute(
                Expression.Call(
                    typeof(Queryable), "First",
                    new Type[] { source.ElementType },
                    source.Expression));
        }

Above not required..but added anyway for consistency.

And adding to IEnumerableSignatures

void First()

Query becomes:

var result = mydata
    .GroupBy("ID", "it")
    .Select("it.First()")
    .Select("new (ID, Field1, Field2)");
user3478586
  • 307
  • 4
  • 15
-2

What about this

            var mydata = new[] {
                new { ID = 1, Field1 = "W", Field2 = "L"},
                new { ID = 1, Field1 = "A", Field2 = "B"}, 
                new { ID = 2, Field1 = "A", Field2 = "B"}, 
                new { ID = 2, Field1 = "C", Field2 = "D"}
            }.ToList();


            var result = mydata.GroupBy(x => x.ID)
                .Select(x => x.ToList()).ToList();​

            var output = result[0][0];

            var output1 = result[0];
            var output2 = result[1];
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • This question is specifically about [tag:dynamic-linq], which allows to [build string based query expressions](http://stackoverflow.com/tags/dynamic-linq/info), not about standard expression(delegate)-based LINQ. – Eugene Podskal May 29 '15 at 21:10
  • I created sample data for testing. Should work on dynamic expressions. – jdweng May 29 '15 at 21:18
  • This code is not dynamic-linq. Dynamic LINQ deals with strings. Not with expressions or delegates. – Eugene Podskal May 29 '15 at 21:19
  • I disagree. Using ToList() makes it a dynamic expression. I think the real issue is using ToList() in the GroupBy is the solution, Then var output = result.FirstOrDefault() gives correct results. – jdweng May 30 '15 at 05:42
  • You can take a look at [another answer](http://stackoverflow.com/a/30540462/3745022) that really uses [tag:dynamic-linq]. Have you read the [tag info](http://stackoverflow.com/tags/dynamic-linq/info)? No offense, but what you propose does not use the technology required by OP. – Eugene Podskal May 30 '15 at 07:32