c# linq to sql iterating through left join results

2019-09-08 04:59发布

问题:

I have a query that looks like this:

var emp = (from activeWO in context.ActiveWOs
                               join activeWOUpdated in context.ActiveWOUpdatedTimes on activeWO.PW_ID equals activeWOUpdated.PW_ID into dj
                               from activeWOUpdated in dj.DefaultIfEmpty()
                               where activeWO.WODC.Contains("IDC") 
                    select new { activeWO.WO_Status,activeWO.PW_ID, activeWO.T_Number, activeWOUpdated.CALCActiveTimeSec });

I have both tables mapped out in the .dbml file but as you can see above I am not pulling a table class object just but just 3 columns from 2 tables.

How can iterate through the results of emp?

回答1:

How can iterate through the results of emp?

Simplest would be:

foreach(var item in emp)
{
  Console.WriteLine(item.WO_Status);
  //other properties
}


回答2:

You can iterate over the collection using foreach.

foreach (var item in emp)
{
    DoSomething(item.WO_Status);
}