sudhirk
sudhirk

Reputation: 433

Linq on Table View

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

Answers (2)

Prasad Kanaparthi
Prasad Kanaparthi

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

tukaef
tukaef

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

Related Questions