简体   繁体   中英

Entityframe Linq using Max and Min

I am trying to do this boolean in Entityframe that will use the min and max value for a datetime field. I can do it fine in SQL but In Entityframe I am not sure what I am missing. The error is related to datetime between bool.

In SQL

MeetingDate=case when Convert(varchar(10),min(md.StartDate),101)=Convert(varchar(10),max(md.StartDate),101) then Convert(varchar(10),min(md.StartDate),101) else Convert(varchar(10),min(md.StartDate),101)+' - '+Convert(varchar(10),max(md.StartDate),101) end

EF

   var listofMeeting = (from m in db.Meetings
                            join md in db.MeetingDates on m.MeetingId equals md.MeetingId
                            join mf in db.MeetingFiles on md.MeetingId equals mf.MeetingId
                            join mv in db.MeetingVersions on mf.MeetingFileId equals mv.MeetingFileId
                            join fm in db.FileManagers on mf.FileManagerId equals fm.FileManagerId
                            join g in db.vwGuidelinePanels on m.GroupId equals g.GroupId
                            where g.GroupId == groupID && mf.FileCategoryItemDictionaryId == 755
                            select new PresentationLayer.Models.GuidelineVersion
                            {
                                GroupId = g.GroupId,
                                MeetingId = md.MeetingId,
                                MeetingDate = (md.StartDate). == max(md.StartDate) ? md.StartDate : min(md.StartDate) == max(md.StartDate),
                            }).GroupBy(g => new
                            {
                                g.MeetingId,
                                g.GroupId
                            }).ToList();

In your SQL, min and max only work because you're either doing a group by later on, or you're not selecting any non-aggregate values in your query.

In LINQ, you need to do the Group By first.

If you have nothing to group by (you want to put everything into one giant group), just use a constant, like 1 in your group by .

group md by 1 into g
let minDate = g.Min(md => md.StartDate)
let maxDate = g.Max(md => md.StartDate)
select new Meeting
{
    MeetingDate = minDate == maxDate ? minDate.ToString() : minDate + " - " + maxDate
}

Update

Based on the updated question:

from m in db.Meetings
join md in db.MeetingDates on m.MeetingId equals md.MeetingId
join mf in db.MeetingFiles on md.MeetingId equals mf.MeetingId
join mv in db.MeetingVersions on mf.MeetingFileId equals mv.MeetingFileId
join fm in db.FileManagers on mf.FileManagerId equals fm.FileManagerId
join g in db.vwGuidelinePanels on m.GroupId equals g.GroupId
where g.GroupId == groupID && mf.FileCategoryItemDictionaryId == 755
group new { md.StartDate /* add other data you're need here as necessary */}
    by new
    {
        g.MeetingId,
        g.GroupId
    } into grp
let minDate = grp.Min(md => md.StartDate)
let maxDate = grp.Max(md => md.StartDate)
select new PresentationLayer.Models.GuidelineVersion
{
    GroupId = grp.Key.GroupId,
    MeetingId = grp.Key.MeetingId,
    MeetingDate = minDate == maxDate ? minDate.ToString() : minDate + " - " + maxDate
}

I'd also recommend you look into using navigation properties to avoid all those explicit joins. You'll likely find that it simplifies the query, once you start thinking of the problem from a LINQ-first perspective instead of trying to directly translate from SQL. That might turn into something more like this:

from g in db.vwGuidelinePanels
from m in g.Meetings
where m.MeetingFiles.Any(mf => mf.FileCategoryItemDictionaryId == 755)
let minDate = m.MeetingDates.Min(md => md.StartDate)
let maxDate = m.MeetingDates.Max(md => md.StartDate)
select new PresentationLayer.Models.GuidelineVersion
{
    GroupId = g.GroupId,
    MeetingId = m.MeetingId,
    MeetingDate = minDate == maxDate ? minDate.ToString() : minDate + " - " + maxDate
}

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