如何使用班级使linq分组工作

时间:2018-10-22 10:07:42

标签: c# linq linq-to-objects

我有这个班级的名单

public class TB_EDS_LOG
{
    public string CLASS_NAME { get; set; }
    public string FIELD { get; set; }
    public Nullable<decimal> OID { get; set; }
    public string CODE_OF { get; set; }
    public string OLD_VALUE { get; set; }
    public string NEW_VALUE { get; set; }
    public string ACTION { get; set; }
    public Nullable<short> ISPASS { get; set; }
    public string RESAULT { get; set; }
    public string USER_ID { get; set; }
    public Nullable<System.DateTime> DATEOF { get; set; }
    public string EDS_ID { get; set; }
    public decimal ID { get; set; }
    public Nullable<decimal> F1 { get; set; }
    public string FK_GISTABLECODE { get; set; }
    public string FK_GISCODE { get; set; }
    public Nullable<decimal> ZID { get; set; }
}

我想使用linq将这些列表按两列分组。所以我用这个课:

private class ClassOid
{
    public string CLASS_NAME { get; set; }
    public decimal? OID { get; set; }
}

,查询语法为:

from dataGiven in data
                    group dataGiven by new ClassOid
                    {
                        CLASS_NAME = dataGiven.CLASS_NAME,
                        OID = dataGiven.OID
                    };

,但这不能正常工作,它返回所有行。当我使用匿名类型时,效果很好。

from dataGiven in data
                    group dataGiven by new
                    {
                        CLASS_NAME = dataGiven.CLASS_NAME,
                        OID = dataGiven.OID
                    };

我必须只使用匿名类型而不使用类类型吗?

编辑:

我已经覆盖了Equals和GetHashCode,如下所示。但仍然没有运气。

    public override bool Equals(object other)
    {
        var classOid = other as ClassOid;
        return classOid != null && (string.Equals(CLASS_NAME, classOid.CLASS_NAME) && OID == classOid.OID);
    }

    public override int GetHashCode()
    {
        return CLASS_NAME.GetHashCode()
               ^ OID.GetHashCode();
    }

0 个答案:

没有答案