Convert Dictionary to List
I have a list h开发者_Go百科ere generated from a long query which the data is used to pump to a report.
var result =
from..
where..
select new { sale.Id, Username = u.Name, Amount = p.amount};
output = result.Tolist();
However, i would like to get rid of that long query by creating my own list of value instead. So i create a dictionary to store the key and value. My purpose is to convert a dictionary back to a list which has the same output like the query above so that it can be pumped to my report
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;
}
I got an error at list.Add(result);
Error 5 Argument '1': cannot convert from 'System.Collections.Generic.Dictionary' to 'System.Collections.Generic.KeyValuePair'
---EDIT------------
I have changed toList<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
and it solved my error.
However i am still getting the value in dictionary format
This should be the intended output format in List
Edit: Changed answer, now that the OP's original error is resolved.
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"]});
}
Really, you should probably refactor this method, and the one you are trying to replace/cache so that it returns a well-known object (not an anonymous type). It could return List<Sale>
or something.
I'm not quite sure what you are trying to achieve to translate the whole thing into a key-value pair list when you actually want a list of objects back. Your function as given can be reduced to:
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();
}
@agent-j is right, you have to store each dictionary; however, if you want a list of key value pairs instead, you can use:
list.AddRange(dicionary.ToList());
Which converts the dictionary to List<KeyValuePair<string, object>>
form. This would duplicate the user name and amount keys many times. I think the dictionary approach is what you want, just throwing it out there in case.
HTH.
What about:
var list = new List<SalesOrder>(dictionary.Values);
or if you have already a list created:
list.AddRange(dictionary.Values);
Make it a list of ExpandoObject
. You can use them in a similar fashion as you do anonymous objects except it is mutable or you can use them as a dictionary (interface).
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
精彩评论