user3291289
user3291289

Reputation: 70

combine foreach with linq

How to change the following code to one LINQ query :

var items = (from s in db.WfRADocStates.Include(x => x.WfStateStatu)
                                       .Include(xx => xx.WfState)
                                       .Include(xxx => xxx.WfState.Dept)
                                       .Include(d => d.RADoc)
             where s.RADocID == docID
                && !s.isHistory
                && s.StatusID == 1
                && s.WfStateStatu.isToNotify
             select s).ToList();

foreach (var item in items)
{
    EmailHelper.notify(item);
}

Upvotes: 3

Views: 525

Answers (2)

evanmcdonnal
evanmcdonnal

Reputation: 48114

Give this a try (excluding all this include nonsense);

Collection.Where(s => s.RadocID == docID && ...).ToList().ForEach(s => EmailHelper.notify(s));

Upvotes: 1

Maddy
Maddy

Reputation: 300

Use .ForEach in the LINQ :

(from s in db.WfRADocStates.Include(x=>x.WfStateStatu).Include(xx=>xx.WfState).Include(xxx=>xxx.WfState.Dept).Include(d=>d.RADoc)
         where
            s.RADocID == docID &&
            !s.isHistory &&
            s.StatusID == 1 &&
            s.WfStateStatu.isToNotifyCNH
         select s).ToList().ForEach(
                s => EmailHelper.notify(s)
         );

Upvotes: 2

Related Questions