Is it possible to make 1 general method from these 2 linq statements?

I have 2 linq statements - currently located in the middle of the switch block. Statements below.

pwStartTime = lender.ApplicationWindows
.Where(w => w.Name == pwPeriod && w.EndTime.TimeOfDay > dateToCheck.TimeOfDay)
.Select(w => w.StartTime)
.FirstOrDefault();


pwStartTime = lender.TransferWindows
.Where(w => w.Name == pwPeriod && w.EndTime.TimeOfDay > dateToCheck.TimeOfDay)
.Select(w => w.StartTime)
.FirstOrDefault();

As you can see, the only difference is that they relate to two different properties of the “lender”, however, all the elements used in the linq query are identical in “ApplicationWindows” and “TransferWindows”, although they do not match objects and each of them contains other unique properties.

So, is it possible to return w.StartDate through one common method?

Thanks in advance.

// There are 2 classes

public class ApplicationWindow
{
    public string Name { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

public class TransferWindow
{
    public string Name { get; set; }

    public DateTime StartTime { get; set; }

    public DateTime EndTime { get; set; }

    [XmlIgnore]
    public TimeSpan Cycle { get; set; }

    [Browsable(false)]
    [XmlElement("Cycle")]
    public string CycleString
    {
        get { return XmlConvert.ToString(Cycle); }
        set { Cycle = value.IsNullOrEmpty() ? TimeSpan.Zero : XmlConvert.ToTimeSpan(value); }
    }
}
+5
source share
3 answers

TransferWindow ApplicationWindow, ? - (, lender.TransferWindows lender.ApplicationWindows - , ):

public DateTime GetStartTime(IList<Window> windows, DateTime dateToCheck, string pwPeriod)
{
    return windows
    .Where(w => w.Name == pwPeriod && w.EndTime.TimeOfDay > dateToCheck.TimeOfDay)
    .Select(w => w.StartTime)
    .FirstOrDefault();
} 
+1

ApplicationWindows TransferWindows , :

public DateTime GetStartTime(IEnumerable<IWindow> windows, string pwPeriod, TimeSpan timeOfDay)
{
    return windows.Where(w => w.Name == pwPeriod && w.EndTime.TimeOfDay > timeOfDay)
        .Select(w => w.StartTime)
        .FirstOrDefault();
}

UPDATE

LINQ To SQL, , Window ( ), , .

, , , VB.NET.

+5

, :

pwStartTime = lender.ApplicationWindows.FirstOrDefaultDateTime(pwPeriod, dateToCheck);
pwStartTime = lender.TransferWindows.FirstOrDefaultDateTime(pwPeriod, dateToCheck);

public interface IWindow
{
    string Name { get; }
    public DateTime StartTime { get; }
    public DateTime EndTime { get; }
}

public class ApplicationWindow : IWindow
{
    public string Name { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

public class TransferWindow : IWindow
{
    public string Name { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

public static class IWindowExt
{
    public static DateTime FirstOrDefaultDateTime(this IEnumerable<IWindow> windows, string pwPeriod, DateTime dateToCheck)
    {
        return windows
                .Where(w => w.Name == pwPeriod && w.EndTime.TimeOfDay > dateToCheck.TimeOfDay)
                .Select(w => w.StartTime)
                .FirstOrDefault();
    }
}

, , .

+3

All Articles