Reputation: 9963
I am trying to bind a grouped linq statement to a asp.net listview. Problem is when the result is been returned to my list view my result look like this.
System.Linq.Enumerable+WhereEnumerableIterator1[TransactionDetail] 20.5 System.Linq.Enumerable+WhereEnumerableIterator
1[TransactionDetail] 7.3
My code is nothing special
List<TransactionDetail> transactions = new List<TransactionDetail>();
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 2, TransactionID = 1, IsPurchase = true });
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 2.2, TransactionID = 2, IsPurchase = true });
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 2.9, TransactionID = 3, IsPurchase = false });
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 4, TransactionID = 4, IsPurchase = true });
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 2.2, TransactionID = 5, IsPurchase = false });
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 5, TransactionID = 6, IsPurchase = true });
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 5, TransactionID = 7, IsPurchase = true });
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 2.3, TransactionID = 8, IsPurchase = true });
transactions.Add(new TransactionDetail { PurchaseDate = DateTime.Now, TransactionAmount = 2.2, TransactionID = 9, IsPurchase = false });
var grouped = from t in transactions
group t by t.IsPurchase into g
select new {
AddedPurchase = g.Where(s=>s.IsPurchase),
Balance = g.Sum(s=>s.TransactionAmount)
};
lstTransactions.DataSource = grouped;
lstTransactions.DataBind();
My listview looks somthing like this
<ItemTemplate>
<span> <%# Eval("AddedPurchase")%></span><span> <%# Eval("Balance")%></span>
</ItemTemplate>
Why am I seeing the System.Linq.Enumerable+WhereEnumerableIterator1[WebUI.TransactionDetail] 20.5 with my returned result. I am only interested in AddedPurchase and Balance
Upvotes: 0
Views: 151
Reputation: 19080
I cant offer a good explanation as to why this is happening but you might like to try the following two fixes:
Add ".ToList()" to the end of your LINQ query. This should stop you seeing the "WhereEnumerableIterator1" type description from popping up.
Add a further LINQ query which can select a new list of ListItems from your grouping:
lstTransactions.Items.AddRange( grouped.Select(x => new ListItem(x.AddedPurchase, x.Balance) )
EDIT:
The problem in fact lies with your second LINQ query, where you select out the new anonymous type into the variable grouped:
AddedPurchase = g.Where(s=>s.IsPurchase)
Added purchase will have the value of a Where() iterator. Im not sure what the purpose of the "AddedPurchase" variable is but if you for example wanted to signify a purchse had been made, then you could do:
AddedPurchase = g.Where(s=>s.IsPurchase).Count() > 0
Upvotes: 1