加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#中类似于SQL’in’关键字的条件

发布时间:2020-12-15 06:31:56 所属栏目:百科 来源:网络整理
导读:在C#中有一个简写的写法: public static bool IsAllowed(int userID){ return (userID == Personnel.JohnDoe || userID == Personnel.JaneDoe ...);} 喜欢: public static bool IsAllowed(int userID){ return (userID in Personnel.JohnDoe,Personnel.Jan
在C#中有一个简写的写法:
public static bool IsAllowed(int userID)
{
    return (userID == Personnel.JohnDoe || userID == Personnel.JaneDoe ...);
}

喜欢:

public static bool IsAllowed(int userID)
{
    return (userID in Personnel.JohnDoe,Personnel.JaneDoe ...);
}

我知道我也可以使用开关,但是大概有50个这样的功能,我必须写(将一个经典的ASP站点移植到ASP.NET),所以我想保持尽可能短.

解决方法

这个怎么样?
public static class Extensions
{
    public static bool In<T>(this T testValue,params T[] values)
    {
        return values.Contains(testValue);
    }
}

用法:

Personnel userId = Personnel.JohnDoe;

if (userId.In(Personnel.JohnDoe,Personnel.JaneDoe))
{
    // Do something
}

我不能要求这个信用,但我也不记得我在哪里看到了.所以,信用你,匿名互联网陌生人.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读