none
GroupBy with System.Linq.Dynamic library RRS feed

  • Question

  • I'm trying to do a dynamic groupby on a IQueryable<T> object. It was failing with my dynamic strings so I tried just hardcoding one to see and it still fails. Didn't know if anyone had any tips for using groupby for System.Linq.Dynamic libraries. Here are the different things I've tried. I'm trying to find duplicate values for dynamic columns.

    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select(columnName);
    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select("new(" + columnName + ")");
    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select("new(Key as " + columnName + ")");
    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select("new(" + columnName + ",Count() as Amount)");
    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select("new(Key as " + columnName + ",Count() as Amount)");

    did all with the GroupBy("new(columnName)","it") also....Everything I've tried fails with an error of

    "No property or field '<<columnName>>' exists in type 'IGrouping`2'"

    where <<columnName>> is the value of the dynamic column string being passed, or if Key is used in the select it will have that instead. I've been working on this for hours and am on a short deadline. Any ideas???? (P.S. I know the columns are in the object being queried)

    Thanks so much.
    Friday, January 15, 2010 8:29 PM

Answers

  • If you're grouping by a single field, try:

    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select("Key");
    ...or...
    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select("it");

    ...if you're grouping by more than one field, GroupBy("new(" + columnName + ", " + columnName2 + ")","it") should do it.


    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
    • Marked as answer by acfalcon2001 Tuesday, January 19, 2010 6:23 PM
    Monday, January 18, 2010 3:38 AM
    Answerer

All replies

  • If you're grouping by a single field, try:

    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select("Key");
    ...or...
    var results = TownList.AsQueryable().GroupBy(columnName,"it").Where("Count() > 1").Select("it");

    ...if you're grouping by more than one field, GroupBy("new(" + columnName + ", " + columnName2 + ")","it") should do it.


    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
    • Marked as answer by acfalcon2001 Tuesday, January 19, 2010 6:23 PM
    Monday, January 18, 2010 3:38 AM
    Answerer
  • Hi

    for grouping multiple field it is throwing error 'The type initializer for 'System.Linq.Dynamic.ClassFactory' threw an exception.' How I resolve this error.

    My Query is

    string a = "Payment";
                    string b = "Facility";


                    IQueryable<Result> res = newlyCreatedObjectCollection.AsQueryable()
                        .GroupBy("new(" + a + ", " + b + ")", "it")
        .Select<Result>("new (Key as Group, Sum(" + YValuePlottingProperty + ") as TotalValue)");

    Waiting for your reply,

    thank you

    Amit


    Amitsp

    Monday, April 23, 2012 1:12 PM