简体   繁体   中英

More elegant way to get the next meeting date

Is there a more elegant way to do what I'm trying to do below? Get the date of the next meeting using linq.

In Model ODM has NextMeeting property (GET) that will be displayed on a display.

[EDITED]

Possibilities of errors:

There's no Meetings in OdmMeetiings (empty).

There's no Meetings in OdmMeetiings with date > today.

(I translate all code names To english to help understand, if you found somethin wrong you can edit)

namespace Models
{
    public partial class ODM
    {
        public ODM()
        {
             this.OdmMeetiings = new List<OdmMeeting>();
        }

        public int Id  { get;  set; }


        public String NextMeeting //There is a more elegant way to do this. 
        {
            get
            {
                var OdmReuniao = this.OdmMeetiings
                    .Where(x => x.Meeting != null && x.Meeting.StartDate > DateTime.Now)
                    .Select(x => x.Meeting)
                    .ToList() //If is possible, without use ToList
                    .Min(x => x.StartDate)
                    ;


                return OdmReuniao.ToShortDateString();
            }
        }

        public virtual ICollection<OdmMeeting> OdmMeetiings { get; set; }

        }
}

namespace Models
{
    public partial class OdmMeeting
    {
        public int IdMeeting { get; set; }
        public int IdODM { get; set; }
        public virtual ODM ODM { get; set; }
        public virtual Meeting Meeting { get; set; }
    }
}

namespace Models
{
    public partial class Meeting
    {
        public Meeting()
        {
            this.ODMs = new List<OdmMeeting>();
        }

        public int Id { get;  set; }

        public System.DateTime StartDate { get; set; }

        public virtual ICollection<OdmMeeting> ODMs { get; set; }

    }
}

Rather than calling the ToList you can order by the query and then get the first element.

var OdmReuniao = this.OdmMeetiings
.Where(x => x.Meeting != null && x.Meeting.StartDate > DateTime.Now)
.Select(x => x.Meeting)
.OrderBy(x => x.Meeting.StartDate)
.FirstOrDefault();

obviously you can order the date as per your requirement.

FirstOrDefault() will return the first element or the default for the type (generally a null).

Edit : updated to account for possibility that collection may be empty and possibility that there may be no OdmMeeting(s) with non null Meeting references. Updated to filter out null meeting references before sorting.

OdmMeeting odmMeeting = this.OdmMeetiings
                    .Where(x => x.Meeting != null)
                    .OrderBy(x => x.Meeting.StartDate)
                    .FirstOrDefault(x => x.Meeting.StartDate > DateTime.Now);

return odmMeeting != null ? odmMeeting.Meeting.StartDate.ToShortDateString() : "-";

Also since you're just after 1 element, and don't really need to reformat the output of an entire collection Select() seems like overkill.

I think this is what you are looking for.

First, you need to test whether Meetings have items. The this.OdmMeetiings.Count property shows this.

Second: You need to test the StartDate > DateTime.Now condition to know whether to return at least one item (if you want to skip the time, use the Date property).

Third: Not need to order to get the next date, Min already does this.

The code below:

    public string NextMeeting
    {
        get
        {
            return this.OdmMeetiings.Count > 0 
                    && this.OdmMeetiings.Any(x => x.Meeting.StartDate.Date > DateTime.Now.Date) ?
                    this.OdmMeetiings.Where(x => x.Meeting.StartDate.Date > DateTime.Now.Date).Min(x => x.Meeting.StartDate).ToShortDateString() 
                    : "-"; 
        }
    }

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