locked
Linq GroupBy Extension: How to retain List<Type> ??? RRS feed

  • Question

  • User360138929 posted


    List<Jurisdiction> jurisdictions = Jurisdiction.GetJurisdictionByOrderId(this.OrderId);
    
    jurisdictions = jurisdictions.GroupBy(p => p.County).ToList();


    I need to use GroupBy on my type List<OrderJurisdiction>. I've found hundreds of examples, but they all yield a var type.
    I CAN NOT USE VAR. I NEED TO KEEP MY DATA IN LIST<TYPE> FORMAT
    Any and all help would be greatly appreciated. This has been a problem for a while now and I've yet to find a solution.
    Tuesday, December 21, 2010 11:23 AM

Answers

  • User1224194097 posted

    Do like this

    List<MyClass>result = (from a in MyTable
                    group a by new { a.ID } into g
                    select new MyClass()
                    {
                        Id = g.Key,
                        mycolumn=g.Max(x=>x.mycolumn)
                    }).ToList();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 21, 2010 1:02 PM