LINQ&枚举作为 IQueryable

发布于 2024-08-21 01:16:34 字数 848 浏览 10 评论 0原文

我基本上有一个枚举

public enum WorkingDays
    {
        Monday, Tuesday, Wednesday, Thursday, Friday
    }

,想与输入进行比较,输入恰好是一个字符串

//note lower case
string input = "monday";

我能想到的最好的事情就是这样

WorkingDays day = (from d in Enum.GetValues(typeof(WorkingDays)).Cast<WorkingDays>()
                               where d.ToString().ToLowerInvariant() == input.ToLowerInvariant()
                               select d).FirstOrDefault();

有没有更好的方法可以做到这一点?

编辑:谢谢亚伦和杰森.但是如果解析失败怎么办?

if(Enum.IsDefined(typeof(WorkingDay),input))//cannot compare if case is different
            {
                WorkingDay day = (WorkingDay)Enum.Parse(typeof(WorkingDay), input, true);
                Console.WriteLine(day);
            }

I basically have an enum

public enum WorkingDays
    {
        Monday, Tuesday, Wednesday, Thursday, Friday
    }

and would like to do a comparison against an input, which happens to be a string

//note lower case
string input = "monday";

The best thing I could come up with was something like this

WorkingDays day = (from d in Enum.GetValues(typeof(WorkingDays)).Cast<WorkingDays>()
                               where d.ToString().ToLowerInvariant() == input.ToLowerInvariant()
                               select d).FirstOrDefault();

Is there any better way to do it ?

Edit: Thanks Aaron & Jason. But what if the parse fails ?

if(Enum.IsDefined(typeof(WorkingDay),input))//cannot compare if case is different
            {
                WorkingDay day = (WorkingDay)Enum.Parse(typeof(WorkingDay), input, true);
                Console.WriteLine(day);
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乄_柒ぐ汐 2024-08-28 01:16:34

您是否正在尝试将 string 转换为 WorkingDays 的实例?如果是这样,请使用 Enum.Parse< /a>:

WorkingDays day = (WorkingDays)Enum.Parse(typeof(WorkingDays), "monday", true);

这里我们使用重载 Enum.Parse(Type , string, bool) 其中 bool 参数指示是否忽略大小写。

另外,您应该将 WorkingDays 重命名为 WorkingDay。看看像这样。例如,当您有 WorkingDay 的实例时,请

WorkingDay day = WorkingDay.Monday;

注意 day 是工作日(因此 WorkingDay),而不是工作日(因此不是 <代码>工作日)。有关命名枚举的其他准则,请参阅枚举类型命名准则< /a>.

Are you trying to convert a string to an instance of WorkingDays? If so use Enum.Parse:

WorkingDays day = (WorkingDays)Enum.Parse(typeof(WorkingDays), "monday", true);

Here we are using the overload Enum.Parse(Type, string, bool) where the bool parameter indicates whether or not to ignore case.

On a side note, you should rename WorkingDays to WorkingDay. Look at like this. When you have an instance of WorkingDay, say,

WorkingDay day = WorkingDay.Monday;

note that day is a working day (thus WorkingDay) and not working days (thus not WorkingDays). For additional guidelines on naming enumerations, see Enumeration Type Naming Guidelines.

蓦然回首 2024-08-28 01:16:34

这是一种非 Linq 方式。

编辑:这基本上是杰森的方式。他在我之前发帖。给他点赞。

Here's a non-Linq way.

EDIT: It's basically Jason's way. He posted before me. Give the kudos to him.

棒棒糖 2024-08-28 01:16:34

使用 IsDefined
链接文本

use IsDefined
link text

忆梦 2024-08-28 01:16:34

我能够通过填充列表集合将枚举转换为 IQueryable。

// Create list to hold enum values
List<string> WorkingDaysList = new List<string>();

// loop thru enum values and store in List
foreach (var value in Enum.GetValues(typeof(WorkingDays)))
{
    var _WorkingDaysList = ((WorkingDays)value).ToString();
    WorkingDaysList.Add(_WorkingDaysList);
}
// use linq to query list       
var result = (from d in WorkingDaysList where d.ToLower() == input.ToLower() select d).FirstOrDefault();

I was able to convert an enum into a IQueryable by populating a List collection.

// Create list to hold enum values
List<string> WorkingDaysList = new List<string>();

// loop thru enum values and store in List
foreach (var value in Enum.GetValues(typeof(WorkingDays)))
{
    var _WorkingDaysList = ((WorkingDays)value).ToString();
    WorkingDaysList.Add(_WorkingDaysList);
}
// use linq to query list       
var result = (from d in WorkingDaysList where d.ToLower() == input.ToLower() select d).FirstOrDefault();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文