在LINQ C#中按多列分组

时间:2019-04-13 11:16:51

标签: c# sql-server

我尝试了linq GroupBy,但颜色重复出现。 如果颜色相同则不希望重复,然后增加值。 下面是我的代码和结果。 请指出错误之处。

if (dt.Rows.Count > 0)
{
    var dtGroup = dt.AsEnumerable().GroupBy(row => new
    {
        FUEL_USED = row.Field<string>("FUEL_USED")
    }).Select(group => new
    {
        FUEL_USED = group.Key.FUEL_USED
    });
    response = new CarsToBeOrdersResponse();
    FuleDetails objfule = new FuleDetails();

    foreach (var key in dtGroup)
    {
        objfule = new FuleDetails();
        objfule.fule = CommonFunction.ParseString(key.FUEL_USED);

        var dtKeyFigures = dt.AsEnumerable().Where(row => row.Field<string>("FUEL_USED") == key.FUEL_USED).GroupBy(row => new
        {
            VEH_VARIANT = row.Field<string>("VEH_VARIANT"),
            VARIANT_DESCRIPTION = row.Field<string>("VARIANT_DESCRIPTION")
        }).Select(group => new
        {
            VEH_VARIANT = group.Key.VEH_VARIANT,
            VARIANT_DESCRIPTION = group.Key.VARIANT_DESCRIPTION,

        });

        foreach (var keyFigure in dtKeyFigures)
        {
            VariantDetail objVariant = new VariantDetail();
            objVariant = new VariantDetail();
            objVariant.variantDesc = CommonFunction.ParseString(keyFigure.VARIANT_DESCRIPTION);
            ColorDetails objColor = new ColorDetails();
            ValueList obj = new ValueList();


            foreach (DataRow dr in dt.Rows)
            {

                if (keyFigure.VEH_VARIANT == CommonFunction.ParseString(dr["VEH_VARIANT"]))
                {
                    objColor = new ColorDetails();
                    objColor.chartKey = CommonFunction.ParseString(dr["VEH_COLOR"]);


                    obj = new ValueList();
                    obj.value = CommonFunction.ParseString(dr["RPO_COUNT"]);
                    objColor.valueList.Add(obj);



                    objVariant.chartDetails.Add(objColor);
                }

            }
            objfule.variantDetails.Add(objVariant);

        }
        response.carsToBeOrdered.Add(objfule);

0 个答案:

没有答案