如何LINQ区分多个字段没有匿名类型

如何LINQ区分多个字段没有匿名类型

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

问题描述

我有一个名为 CLASS 的表格

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

我有以下记录


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


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


现在我使用下面的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

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

So it should return me the following 2 Distinct Records

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..

RegradsPradeep

解决方案

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区分多个字段没有匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:27