fahad ayub
fahad ayub

Reputation: 21

Extracting data from two Tables using LINQ

I got two tables OrderHead and Labeldata, both these two tables don't have any relationship. I want to extract data from these two table, I am using following LINQ:

var ODetail = (
    from o in oContext.OrderHeads
    join l in oContext.LIT04LABELDATA on o.CUSTORD equals l.ORDERNUMBER
    where l.ORDERNUMBER == oNumber
    select new { LIT04LABELDATA = l, OrderHead = o }
).ToList();

I want to use the data from both these tables, but I don't know how to extract data from the variable oDetail, any suggestions?

Upvotes: 2

Views: 906

Answers (2)

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4908

var b=oContext.OrderHeads
.Where(p => oContext.LIT04LABELDATA.Where(q=>q.ORDERNUMBER==oNumber)
      .Select(q => q.ORDERNUMBER).Contains(p.CUSTORD))
.Select(p => new
{
    LIT04LABELDATA = oContext.LIT04LABELDATA
       .FirstOrDefault(q=>q.ORDERNUMBER==p.CUSTORD),
    OrderHead =p,
}).ToList();

Upvotes: 0

Dave
Dave

Reputation: 3621

You're creating an anonymous type which has two types as its properties. Unlike in a SQL query, the hierarchy is preserved here so oDetail won't contain CUSTORD but oDetail.OrderHead will. You need to query as:

foreach(var detail in ODetail)
{
    Console.WriteLine( detail.LIT04LABELDATA.ORDERNUMBER + "   " + detail.ORDERHEAD.CUSTORD );
}

Upvotes: 2

Related Questions