iT邦幫忙

0

LINQ 下GROUP 問題?

    static Random Rnd = new Random();
    static DataRowCollection GetTableByDetail()
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("MATERIAL", typeof(string));
        table.Columns.Add("DESC_SP", typeof(string));
        for (int i = 0; i < 3; i++)
        {
            string name = PEOPLES[Rnd.Next(0, 8)];
            table.Rows.Add(i, name, "N/A");
        }
        return table.Rows;
    }

static void Main(string[] args){
    #region 準備測試資料 
    DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(string));

    table.Rows.Add(10, "Indocin", "David");
    table.Rows.Add(15, "Enebrel", "Sam");
    table.Rows.Add(20, "Hydralazine", "Christoff");
    table.Rows.Add(25, "Combivent", "Janet");
    table.Rows.Add(30, "Dilantin", "Melanie");
    var queryResult = new List<AVO>();
    foreach (DataRow dr in dt.Rows)
    {
        int iii = Rnd.Next(0, 8); 
        var tmp = new AVO() { ID = "ID_"+iii, PART_NO = "PART_"+iii };
        tmp.Header = dr;
        tmp.Detail_List = new List<DataRow>();
        foreach (DataRow drD in GetTableByDetail())
        {
            tmp.Detail_List.Add(drD);
        }
        queryResult.Add(tmp);
    }
    #endregion 
    
        var nameGroup = queryResult.GroupBy(qr => qr.Detail_List.Select(x => x["MATERIAL"]).ToString());
    foreach (var groupObj in nameGroup)
    {
        if (!string.IsNullOrWhiteSpace(groupObj.Key))
        { 
           ... 略 ...
        }
    }
}

請問 LINQ 要針對,第3層的料號,GRPUP BY 要怎樣下?才是對的
/images/emoticon/emoticon06.gif

1 個回答

1
暐翰
iT邦大師 1 級 ‧ 2019-02-22 17:00:54
最佳解答
  1. 不要使用DataTable弱類型,可以宣告一個Material類別
  2. 要使用group by需要轉成明細ViewModel,這跟SQL概念很像:『當你想要group by多方資料又顯示一方資料,需要查詢多方資料+Join一方資料再Group by』
class Demo
{
	public static void Main(string args)
	{
		var testJson = @"
		[
		    {
		        ""ID"":""ID_1"",
		        ""PART_NO"":""PART_1"",
		        ""Detail_List"":[
		            {
		                ""ID"":1,
		                ""MATERIAL"":""Mlot1"",
		                ""DESC_SP"":""N/A""
		            },
		            {
		                ""ID"":2,
		                ""MATERIAL"":""Mlot2"",
		                ""DESC_SP"":""N/A""
		            },
		            {
		                ""ID"":3,
		                ""MATERIAL"":""Mlot3"",
		                ""DESC_SP"":""N/A""
		            }
		        ]
		    },
		    {
		        ""ID"":""ID_2"",
		        ""PART_NO"":""PART_2"",
		        ""Detail_List"":[
		            {
		                ""ID"":1,
		                ""MATERIAL"":""Mlot1"",
		                ""DESC_SP"":""N/A""
		            },
		            {
		                ""ID"":2,
		                ""MATERIAL"":""Mlot2"",
		                ""DESC_SP"":""N/A""
		            },
		            {
		                ""ID"":3,
		                ""MATERIAL"":""Mlot3"",
		                ""DESC_SP"":""N/A""
		            }
		        ]
		    }
		]
		";

		var queryResult = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<AVO>>(testJson);//為了測試方便不使用Json.NET

		//不使用AutoMapper方式
		var materialViewModels = new List<MaterialViewModel>();
		foreach (var avo in queryResult)
		{
			foreach (var material in avo.Detail_List)
			{
				materialViewModels.Add(new MaterialViewModel()
				{
					AVOID = avo.ID,
					AVOPART_NO = avo.PART_NO,
					ID = material.ID,
					DESC_SP = material.DESC_SP,
					MATERIAL = material.MATERIAL
				});
			}
		}

		var nameGroup = materialViewModels.GroupBy(material => material.MATERIAL);
		foreach (var groupObj in nameGroup)
		{
			//你需要的邏輯
		}
	}
}

class AVO
{
	public string ID { get; set; }
	public List<Material> Detail_List { get; set; }
	public string PART_NO { get; set; }
}

class Material
{
	public int ID { get; set; }
	public string MATERIAL { get; set; }
	public string DESC_SP { get; set; }
}

class MaterialViewModel
{
	public int ID { get; set; }
	public string MATERIAL { get; set; }
	public string DESC_SP { get; set; }

	public string AVOPART_NO { get; set; }
	public string AVOID { get; set; }
}

nameGroup結果值:

我要發表回答

立即登入回答