9

I have a table named CLASS with the Fields

    **{BATCH, DEGREE, DEPT, SEM, SECTION, GROUP }** 

I has the Following Records

Record1: { 2009 , B.E , CSE , 3Sem , ASec , 1Group }

Record2: { 2009 , B.E , CSE , 3Sem , ASec , 2Group }

Record3: { 2009 , B.E , ECE , 4Sem , ASec , 1Group }

Record4: { 2009 , B.E , ECE , 4Sem , ASec , 2Group }

I Need to Select Distinct Records Ignoring the GROUP and considering only the following

{BATCH, DEGREE, DEPT, SEM, SECTION }

So it should return me the following 2 Distinct Records

                  Record1: **{ 2009 , B.E , CSE , 3Sem , ASec , 1Group }** 

DistinctRecord: 1------------------------(OR)**

                  Record2: **{ 2009 , B.E , CSE , 3Sem , ASec , 2Group }**

-----(AND)

                  Record3: **{ 2009 , B.E , ECE , 4Sem , ASec , 1Group }**

DistinctRecord: 2------------------------(OR)

                  Record4: **{ 2009 , B.E , ECE , 4Sem , ASec , 2Group }**

Now i'm using the following LINQ Query

    public static object GetDistictClasses(IQueryable<Class> AllClasses)
    {
        return (from c in AllClasses
                group c by new { c.Batch, c.Degree_ID, c.Specialization_ID, c.CurrentSemester, c.Section_ID } into grp
                select new 
                {
                    grp.Key.Batch,
                    grp.Key.Degree_ID,
                    grp.Key.Specialization_ID,
                    grp.Key.CurrentSemester,
                    grp.Key.Section_ID
                }).Distinct();
    }

But it returns me a Ananymous Type, but i need the Actual Class Type

Could some one Help Me... thank You..

Regrads Pradeep

Pradeep
  • 271
  • 1
  • 3
  • 10
  • That should return an `IQueryable` (assuming the way you initialized that is legal for the type). The only anonymous objects here are your keys. p.s., the final call to `Distinct()` is not necessary since the grouping should have taken care of that. – Jeff Mercado May 27 '11 at 07:12

2 Answers2

30

You can do the following:

return
    from c in AllClasses
    group c by new
    {
        c.Batch, 
        c.Degree_ID, 
        c.Specialization_ID, 
        c.CurrentSemester, 
        c.Section_ID 
    } into grp
    select grp.First();

This takes every first Class from the given group, thus returns a Entity object instead of a anonymous type.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Legend. This has completely saved me! Thanks so much. – MartynJones87 Jun 11 '12 at 10:43
  • clarifying question: why, when I use `...group c by new [type] {...} into grp ...` does this not work? I still get all the records instead of just the distinct ones. Is this a problem with the class's GetHashCode method or something? – Keith Apr 04 '17 at 14:43
21

Lets make it simple - distinct is done on two properties:

  • the Id of the user and
  • the group id

also this will return the same object type.

var distinctgroupSubscriptions = groupSubscriptions.GroupBy(item => new { item.User.Id, item.Group.GroupId }).Select(group => group.First()).ToList();
Nogard
  • 1,779
  • 2
  • 18
  • 21
Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101