将foreach与linq结合起来

时间:2014-02-26 04:44:41

标签: c# linq

如何将以下代码更改为一个LINQ查询:

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);
}

2 个答案:

答案 0 :(得分:2)

在LINQ中使用.ForEach:

(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)
         );

答案 1 :(得分:1)

尝试一下(不包括所有这些包括废话);

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