试图在iqueryable中进行切换

时间:2018-05-30 16:53:34

标签: c# linq-to-entities iqueryable

我有五列Cc1,Cc2,Cc3 ...... 我在" Any"中有以下switch语句。检查基于ccLevel的列,但是test1表有3200万行,并且需要永久地获取它们,是否有更好的方法使它更快?

if (_db.Test1.Any(x => CCLevels(x, ccLevel) == cCValue.CcCode))
        {
           some Message...
        }

if (_db.Test2.Any(x => CCLevels(x, ccLevel) == cCValue.CcCode))
        {
           some Message...
        }


private string CCLevels<T>(T x, string ccLevel) where T : IccLevels
    {
        string level = null;
        switch (ccLevel)
        {
            case "1":
                level = x.Cc1;
                break;
            case "2":
                level = x.Cc2;
                break;
            case "3":
                level = x.Cc3;
                break;
            case "4":
                level = x.Cc4;
                break;
            case "5":
                level = x.Cc5;
                break;
        }
        return level;
    }

当我像下面这样做时需要一秒钟,

if (_db.Test1.Any(x => x.Cc1 == cCValue.CcCode))
        {
            some Message...
        }

if (_db.Test1.Any(x => x.Cc2 == cCValue.CcCode))
        {
            some Message...
        }

0 个答案:

没有答案