如何LINQ区分多个字段没有匿名类型 [英] How to LINQ Distinct by Multiple Fields without anonymous types

查看:150
本文介绍了如何LINQ区分多个字段没有匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 CLASS 的表格

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

我有以下记录



Record1: {2009,BE,CSE,3Sem,ASec,1Group} b
$ b

Record2: {2009,BE,CSE,3Sem,ASec,2Group}



Record3: strong> {2009,BE,ECE,4Sem,ASec,1Group}



2Group}



我需要选择不同的记录忽略,并仅考虑以下内容



{BATCH,DEGREE,DEPT,SEM }



所以它应该返回以下 2 Distinct Records



  Record1:** { CSE,3Sem,ASec,1Group} ** 

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

  Record2:** {2009,BE,CSE,3Sem,ASec,2Group} ** 

----- (AND)

  Record3:** { 4Sem,ASec,1Group} ** 

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

  Record4:** {2009,BE,ECE,4Sem,ASec,2Group} ** 



现在我使用下面的LINQ查询

  public static object GetDistictClasses(IQueryable< Class> AllClasses)
{
return(从AllClasses中的c到
组c由新的{c.Batch,c.Degree_ID,c.Specialization_ID,c.CurrentSemester,c.Section_ID}转换成grp
select new
{
grp.Key.Batch,
grp.Key.Degree_ID,
grp.Key.Specialization_ID,
grp.Key.CurrentSemester,
grp.Key.Section_ID
})。Distinct();
}

但它返回一个 Anaymous Type 我需要实际的类类型



可以帮助我...
谢谢..



Regrads
Pradeep

解决方案

您可以执行以下操作:

 返回
从AllClasses中的c
组c由新的
{
c.Batch,
c.Degree_ID,
c.Specialization_ID,
c.CurrentSemester,
c.Section_ID
} into grp
select grp.First();

这会从给定的每个组,因此返回一个Entity对象,而不是一个匿名类型。


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

解决方案

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.

这篇关于如何LINQ区分多个字段没有匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆