I have a list here 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 to List<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

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: