2011-06-26 190 views
2

我在這裏生成了一個列表,這個列表是由一個長查詢生成的,該查詢將數據用於抽取報表。將字典轉換爲列表

var result = 
    from.. 
    where.. 
    select new { sale.Id, Username = u.Name, Amount = p.amount}; 
output = result.Tolist(); 

但是,我想通過創建自己的值列表來擺脫那個長查詢。所以我創建了一個字典來存儲密鑰和價值。 我的目的是轉換字典背到具有類似上面的查詢相同的輸出列表,以便可以抽我的報告

public static object GetSalesDataBySaleOrderID(SaleOrder sale) 
    { 
     List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>(); 

     for (int i = 0; i < sale.saleOrderItem.Count(); i++) 
     {    
      Dictionary<string, object> result = new Dictionary<string, object>() 
      { 
       {sale.Id.ToString(), sale.Id},     
       {"UserName", sale.User.GetSingleUserById(sale.saleOrderItem[i].UserId).Name}, 
       {"Amount", sale.saleOrderItem[i].Amount},     
      }; 

      list.Add(result); 
     } 

     return list; 
    } 

我在list.Add(result);

錯誤得到了一個錯誤5參數 '1':不能轉換從 'System.Collections.Generic.Dictionary' 到 'System.Collections.Generic.KeyValuePair'

---編輯----------- -
我已更改爲List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();並解決了我的錯誤。

但是我仍然在字典格式 enter image description here

所獲得的價值,這應該是在列表中的預期輸出格式 enter image description here

+0

我改變了我的答案。讓我知道它是否符合您的需求。 –

+0

你的要求看起來很奇怪,我不確定我明白你的目標是什麼。你想把你的結果對象,並將它們轉換爲從屬性名稱映射到值的字典?有更有效的方法來獲得這種理想的行爲。實際上,使用[ExpandoObject](http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx)可以免費爲您提供這一切,如果這是您想要的。 –

回答

1

我不太清楚你正在努力實現整個事情轉換成鍵值對列表,當你真正想要的對象列表後面是什麼。您的功能可以降至:

public static object GetSalesDataBySaleOrderID(SaleOrder sale) 
{ 
    return sale.saleOrderItem 
       .Where(s => s != null) 
       .Select(s => new { 
            Id = s.Id, 
            Username = sale.User.GetSingleUserById(s.UserId).Name, 
            Amount = s.Amount 
           }) 
       .ToList(); 
} 
+0

工程很棒。你能請指教如何忽略所有的空值?我嘗試了這一點。選擇(..)。DefaultIfEmpty()。Tolist()但它仍然與對象空錯誤轉換錯誤。 – VeecoTech

+1

@belinq:你必須在'!= null'上添加一個'Where'子句和過濾器。我更新了我的答案。 – ChrisWue

4

編輯:更改答案,現在的OP的原始錯誤得到解決。

public static object GetSalesDataBySaleOrderID(SaleOrder sale) 
{ 
    List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>(); 
    for (int i = 0; i < sale.saleOrderItem.Count(); i++) 
    { 
     Dictionary<string, object> result = new Dictionary<string, object>() 
     { 
      {"SaleId", sale.Id}, 
      {"UserName", sale.User.GetSingleUserById(sale.saleOrderItem[i].UserId).Name}, 
      {"Amount", sale.saleOrderItem[i].Amount}, 
     }; 
     list.Add(result); 
    } 
    return list.Select (d => new { Id=(int)d["SaleId"], Username = d["UserName"], Amount = (Decimal) d["Amount"]}); 
} 

真的,你應該重構這個方法,你試圖取代/緩存,這樣,它返回一個衆所周知的對象(而不是匿名類型)之一。它可能會返回List<Sale>什麼的。

1

@ agent-j是對的,你必須存儲每個字典;但是,如果你想鍵值對的列表,而不是,您可以使用:

list.AddRange(dicionary.ToList()); 

哪個字典轉換爲List<KeyValuePair<string, object>>形式。這會多次複製用戶名和金額密鑰。我認爲字典的方法是你想要的,只是把它扔在那裏以防萬一。

HTH。

1

什麼:

var list = new List<SalesOrder>(dictionary.Values); 

,或者如果你已經創建了一個清單:

list.AddRange(dictionary.Values); 
1

讓它的ExpandoObject列表。您可以像使用匿名對象一樣使用它們,除非它是可變的,或者您可以將它們用作字典(界面)。

var result = myDataContext.MyTable 
    .Where(sale => /* my conditions */) 
    .AsEnumerable() // use LINQ to objects at this point 
    .Select(sale => 
    { 
     dynamic exo = new ExpandoObject(); 
     exo.SaleId = sale.Id; 
     exo.UserName = sale.User.GetSingleUserById(sale.saleOrderItem[i].UserId).Name; 
     exo.Amount = sale.saleOrderItem[i].Amount; 
     return exo as IDictionary<string, object>; 
    }); 

IDictionary<string, object> first = result.First(); 
// access the properties as if it were a dictionary 
int saleId = (int)first["SaleId"]; // get (and cast) the SaleId property 
first["UserName"] = "Bob"; // set the UserName property 

// cast it back to dynamic to access the properties as if it were an anonymous object 
dynamic dfirst = first; 
decimal amount = dfirst.Amount; // get the Amount property (no cast necessary) 
dfirst.Amount = 3238132.12M // set the Amount property