Reputation: 433
I have following type
public class TransactionDetails
{
public int TransID {get; set;}
public List<string> OrderID { get; set; }
}
What should be the Linq query on the following table View (say Trans_View) to fill this TransactionDetails
custom type with following data from view.
View record in following row format:
TransID OrderID
1 ABC
1 DEF
1 IJK
2 XYZ
2 PQR
Upvotes: 0
Views: 250
Reputation: 6563
Try this,
var transactionDetails = Trans_View.GroupBy(x => x.TransID)
.Select(t => new TransactionDetails()
{
TransID = t.Key,
OrderID = t.Select(o => o.OrderID).FirstOrDefault(),
});
Upvotes: 0
Reputation: 9214
Try this:
var transactionDetails = Trans_View.GroupBy(x => x.TransID, x => x.OrderID)
.Select(g => new TransactionDetails()
{
TransID = g.Key,
OrderID = g.ToList()
});
Upvotes: 2