2017-07-06 61 views
0

我想選擇OrderId &從多個字典中提取行並創建一個包含行列表的Orders列表。將多個字典中的值從LINQ中選擇到對象列表中

我這個遠掙扎的代碼是:

var listOfDictionaries = new List<Dictionary<string, string>>(); 

var dict1 = new Dictionary<string, string>(); 
dict1.Add("OrderId", "12345");  
dict1.Add("Row", "1");    
listOfDictionaries.Add(dict1); 

var dict2 = new Dictionary<string, string>(); 
dict2.Add("OrderId", "97845"); 
dict2.Add("Row", "10"); 
listOfDictionaries.Add(dict2); 

var dict3 = new Dictionary<string, string>(); 
dict3.Add("OrderId", "12345"); 
dict3.Add("Row", "2"); 
listOfDictionaries.Add(dict3); 

var grouped = listOfDictionaries.SelectMany(d => 
       d.Where(kvp => 
        kvp.Key.ToLower().Contains("orderid") || kvp.Key.ToLower().Contains("row"))) 
       .GroupBy(a => a.Key).ToList(); 

public class Order 
{ 
    public Order() 
    { 
     OrderRows = new List<OrderRow>(); 
    } 

    public string OrderId { get; set; } 
    public List<OrderRow> OrderRows { get; set; } 
} 

public class OrderRow 
{ 
    public string OrderRowId { get; set; } 
} 

任何人有一個好主意,如何解決這個問題?

+2

SelectMany將他們弄平了這麼一個壞主意。 –

+1

希望dict3之後的代碼在dict3中增加了值,而不是在dict2中。 –

回答

2

您可以先選擇訂單,然後選擇類似的記錄行。

var orderFieldName = "OrderId"; 
var orders = listOfDictionaries.Select(dict => dict[orderFieldName]).Distinct(); 
var grouped = orders.Select(order => new Order 
{ 
    OrderId = order, 
    OrderRows = listOfDictionaries.Where(dict => dict[orderFieldName] == order) 
     .Select(item => new OrderRow {OrderRowId = item["Row"]}).ToList() 
}); 
+0

感謝您的回答。我收到以下異常: System.ArgumentException:具有相同密鑰的項目已添加。 at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary'2.Insert(TKey key,TValue value,Boolean add) at Rextester.Program.Main(String [] args) – Henrik

+0

@Henrik看第三部字典的代碼。您將條目添加到第二個。 'dict2.Add(「Row」,「2」);' – NtFreX

+0

啊,謝謝,該死的複製粘貼.. :) – Henrik

2

你需要做的

var result = listOfDictionaries.Select(x => new { OrderId = x["OrderId"], Row = x["Row"] }) 
        .GroupBy(x => x.OrderId) 
        .Select(x => new { OrderId = x.Key, Values = x.Select(y => y.Row) }); 
0

就像@Nikhil阿格拉瓦爾說:

檢查this

var result = listOfDictionaries 
      .Select(x => new { OrderId = x["OrderId"], Row = x["Row"] }) 
      .GroupBy(x => x.OrderId) 
      .Select(x => new Order{ 
       OrderId = x.Key, 
       OrderRows = x.Select(y => new OrderRow{OrderRowId = y.Row}).ToList() 
      }); 
1

我覺得你的字典代碼將無法運行,因爲它不會允許您在同一個列表中輸入重複鍵值。所以在這裏我創建了一個解決方案使用結構列表

class Stackoverflow 
{ 
    struct OrderData 
    { 
     public string OrderID { get; set; } 
     public string Row { get; set; } 
    } 
    static void Main(string[] args) 
    { 
     string strInputOrderID = "ORD1"; 

     ////Using Struct List 
     List<OrderData> objOrderDataList = new List<OrderData>(); 

     OrderData objOrderData = new OrderData(); 
     objOrderData.OrderID = "ORD1"; 
     objOrderData.Row = "1"; 
     objOrderDataList.Add(objOrderData); 

     objOrderData = new OrderData(); 
     objOrderData.OrderID = "ORD2"; 
     objOrderData.Row = "2"; 
     objOrderDataList.Add(objOrderData); 

     objOrderData = new OrderData(); 
     objOrderData.OrderID = "ORD3"; 
     objOrderData.Row = "3"; 
     objOrderDataList.Add(objOrderData); 

     objOrderData = new OrderData(); 
     objOrderData.OrderID = "ORD1"; 
     objOrderData.Row = "4"; 
     objOrderDataList.Add(objOrderData); 

     List<OrderData> resultOrderDataList = (from data in objOrderDataList 
               where data.OrderID.Equals(strInputOrderID) 
               select data).ToList(); 
     Console.ReadLine(); 
    } 
} 

我已經運行它,它的工作正常。 請檢查並在評論中告訴我。