gpt4 book ai didi

c# - 查找通用列表的方法以搜索特定的星期几

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:52 25 4
gpt4 key购买 nike

我有一个 CallDay 对象的 Generic List,每个 CallDay 对象都有 DayOfWeek List 我想知道 DayOfWeek 列表是否包含特定的一天.这是我的 C# 代码

public class CallDays
{
public CallDays();


public List<DayOfWeek> DayOWList { get; set; }
public bool EveryDayOfWeek { get; set; }
public bool PublicHoliday { get; set; }
}



private void IdentifyDay(List<CallDays> callDayList)
{
// Some condition to set DayOFWeek filter criteria

DayOfWeek dayOfWeekToFind = drInMemory.Start.DayOfWeek;

foreach (var item in CallDayList)
{
// Here I need to find whether item.DayOWList contains DayOfWeek which matches dayOfWeekToFind
item.DayOWList.Find
}
}

最佳答案

您可以使用 Linq 查找包含它的第一个元素。因此,使用方法 FirstOrDefault 并在表达式上找到它,您可以检查 DayOWList 集合中是否有 DayOfWeek 对象,对于示例:

private void IdentifyDay(List<CallDays> callDayList)
{
// Some condition to set DayOFWeek filter criteria

DayOfWeek dayOfWeekToFind = drInMemory.Start.DayOfWeek;

// find a object that contains the dayOfWeekToFind on the DayOWList collection
CallDays result = CallDayList.FirstOrDefault(x => x.DayOWList.Any(d => d == dayOfWeekToFind));


// you can use the result object here
}

关于c# - 查找通用列表的方法以搜索特定的星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29076446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com