简体   繁体   中英

self join Lambda query in Entity Framework

my code is

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

i want do with only lambda query, not foreach. how can i do? Is it possible?

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

I think this should solve your Query

OR

you can also try something like this:

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

Try this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM