higgsy
higgsy

Reputation: 2011

Getting column data in an EntityFramework Join statement

I'm using the EntityFramework (for the first time) and have managed to get as far as querying, updating etc. I've got a linq query that is joining three tables, like so:

//get a list of completed orders between the last run date of this job and NOW
        var completedOrders = from orders in db.orders
                              join orderStatus in db.orderStatus
                              on orders.statusID equals orderStatus.statusID
                              join aspnetUsers in db.aspnet_Users
                              on orders.userID equals aspnetUsers.UserId
                              into joined
                              where (orderStatus.statusID == 2 || orderStatus.statusID == 3 || orderStatus.statusID == 5) && (orders.dateTimeStamp > job.lastRunDate && orders.dateTimeStamp < DateTime.Now || !job.lastRunDate.HasValue)
                              select joined;

Then I've got a simply foreach loop:

foreach (var order in completedOrders)
        {

            //process here
        }

My question is, how do I get at the information within the foreach loop? I need to get at the column data, something like:

order["date"]

Although ideally i'd like it to be strongly typed seeing as Im using the Entity Framework.

Any feedback would be greatly appreciated.

Thanks in advance Al

Upvotes: 0

Views: 474

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109347

You should remove the into and add an anonymous type:

...
on orders.userID equals aspnetUsers.UserId
where (orderStatus.statusID == 2 || orderStatus.statusID == 3 || orderStatus.statusID == 5) && (orders.dateTimeStamp > job.lastRunDate && orders.dateTimeStamp < DateTime.Now || !job.lastRunDate.HasValue)
select new { orders, orderStatus, aspnetUsers }

Then you can loop through the result

 foreach (var x in completedOrders)
 {
   // use x.orders.Date, etc.
 }

Upvotes: 1

Related Questions