How to extract particular columns values from multiple rows using linq

时间:2016-12-02 05:00:14

标签: c# linq

I have a model class like this.

Public Class SampleModel
{
    public string ItemName{ get; set; }
    public string ItemNo{ get; set; }
    public int ItemQty{ get; set; }
    public string GroupName{ get; set; }
    public int Group1 { get; set; }
    public int Group2 { get; set; }
    public int Group3 { get; set; }
    public int Group4 { get; set; }
}

The Table Contains the values for first 4 ItemName,ItemNo,ItemQty and GroupName. for each ItemNo are having 4 rows and only ItemQty will be differ which is entered by each groupName(Group1Count ,Group2Count ,Group3Count ,GroupCount ).

Table content like this .

ItemName    ItemNo  ItemQty GroupName
Pen           234    2        Group1 
Pen           234    4        Group2
Pen           234    6        Group3
Pen           234    3        Group4
item2         365    3        Group1 
item2         365    5        Group2
item2         365    2        Group3
item2         365    3        Group4
item3         370    3        Group1
item3         370    2        Group4
item4         372    6        Group2
item4         372    9        Group4

so i am getting all the values using this query.

var data= from a in context.Batch where a.GroupName != "" select a; 

from this again i need filter out based on the combination of ItemName,ItemNo,ItemQty and show the result like this.

ItemName    ItemNo  Group1  Group2  Group3  Group4 
Pen          234      2       4       6       3
item2        365      3       5       2       3
item3        370      3       0       0       2
item4        372      0       6       0       9

Please suggest me how to write a linq query for this.

3 个答案:

答案 0 :(得分:0)

You can use

    var list=context.Batch.GroupBy(x=>new{x.ItemName,x.ItemNo})
                          .Select(x=>new
                          {
                                  ItemName=x.Key.ItemName,
                                  ItemNo=x.Key.ItemNo,
                                  Group1=x.Where(y=>y.GroupName=="Group1").FirstOrDefault(),
                                  Group2=x.Where(y=>y.GroupName=="Group2").FirstOrDefault(),
                                  Group3=x.Where(y=>y.GroupName=="Group3").FirstOrDefault(), 
                                  Group4=x.Where(y=>y.GroupName=="Group4").FirstOrDefault() 
                           })
                         .Select(x=> new SampleModel{
                                  ItemName=x.ItemName,
                                  ItemNo=x.ItemNo,
                                  Group1=x.Group1==null?0:x.Group1.ItemQty,
                                  Group2=x.Group2==null?0:x.Group2.ItemQty,
                                  Group3=x.Group3==null?0:x.Group3.ItemQty,
                                  Group4=x.Group4==null?0:x.Group4.ItemQty

                            }).ToList();

答案 1 :(得分:0)

var data = context.Batch
    .GroupBy(i => new {i.ItemName, i.ItemNo})
    .Select(g => new {
        ItemName = g.Key.ItemName,
        ItemNo = g.Key.ItemNo,
        Group1 = g.Where(i => i.GroupName == “Group1”).First().ItemQty,
        Group2 = g.Where(i => i.GroupName == “Group2”).First().ItemQty,
        Group3 = g.Where(i => i.GroupName == “Group3”).First().ItemQty,
        Group4 = g.Where(i => i.GroupName == “Group4”).First().ItemQty
    })
    .Where(i => i.GroupName != “”);

答案 2 :(得分:0)

这是正确的解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ItemName", typeof(string));
            dt.Columns.Add("ItemNo", typeof(int));
            dt.Columns.Add("ItemQty", typeof(int));
            dt.Columns.Add("GroupName", typeof(string));

            dt.Rows.Add(new object[] {"Pen",234, 2, "Group1"}); 
            dt.Rows.Add(new object[] {"Pen",234, 4, "Group2"}); 
            dt.Rows.Add(new object[] {"Pen",234, 6, "Group3"}); 
            dt.Rows.Add(new object[] {"Pen",234, 3, "Group4"}); 
            dt.Rows.Add(new object[] {"item2",365, 3, "Group1"}); 
            dt.Rows.Add(new object[] {"item2",365, 5, "Group2"}); 
            dt.Rows.Add(new object[] {"item2",365, 2, "Group3"}); 
            dt.Rows.Add(new object[] {"item2",365, 3, "Group4"});

            List<string> groupNames = dt.AsEnumerable().Select(x => x.Field<string>("GroupName")).Distinct().ToList();

            DataTable pivot = new DataTable();
            pivot.Columns.Add("ItemName", typeof(string));
            pivot.Columns.Add("ItemNo", typeof(int));
            foreach (string groupName in groupNames)
            {
                pivot.Columns.Add(groupName, typeof(string));
            }

            var items = dt.AsEnumerable().GroupBy(x => x.Field<string>("ItemName"));

            foreach (var item in items)
            {
                DataRow pivotRow = pivot.Rows.Add();
                pivotRow["ItemName"] = item.First().Field<string>("ItemName");
                pivotRow["ItemNo"] = item.First().Field<int>("ItemNo");
                foreach (var group in item)
                {
                    pivotRow[group.Field<string>("GroupName")] = group.Field<int>("ItemQty");
                }
            }
        }

    }
}

enter image description here