实体框架中的自联接Lambda查询

本文关键字:Lambda 查询 框架 实体 | 更新日期: 2023-09-27 18:21:16

我的代码是

    var model = dt.FishEventScheduleVaccination
            .Join(
                dt.FishEventSchedule,
                vaccination => vaccination.ScheduleId,
                schedule => schedule.ScheduleId,
                (vaccination, schedule) => new { vaccination, schedule }
            )
            .Select(q=>new {
                q.vaccination,
                q.schedule
            })
            .Where(w=>w.schedule.Start>DateTime.Now).ToList();
        var rtn=new List<FishEventScheduleVaccination>();
        foreach (var m in model) {
            var item = m.vaccination;
            item.FishEventSchedule = m.schedule;
            rtn.Add(item);
        }

我只想使用lambda查询,而不是foreach。我该怎么办?有可能吗?

实体框架中的自联接Lambda查询

试试这个:

dt.FishEventScheduleVaccination
    .Join(dt.FishEventSchedule,
          vaccination => vaccination.ScheduleId,
          schedule => schedule.ScheduleId,
          (vaccination, schedule) => new { vaccination, schedule })
    .Where(w => w.schedule.Start > DateTime.Now)
    .AsEnumerable()
    .Select(q => { q.vaccination.FishEventSchedule = q.schedule; return q.vaccination; })
    .ToList();
from r in Entities.tbl join q in Entities.tbl on r.Id equals q.Id2 select new
{
    Id = r.Column + " " + r.Column, Data = q.Column + " " + q.Column 
};

我认为这应该解决您的查询

你也可以试试这样的东西:

db.DTOPageSets.Join(db.DTOPageSets
                    .AsEnumerable(), a => a.ContentPageID, b => b.CategoryID, (a, b) => a);