C# 中 Linq 查询中的枚举条件

Enum Condition in Linq Query in C#

我想在 Linq 查询中添加 Enum 条件,即使枚举数据成员为 null 或空

我在模型中添加了 ALL 用于过滤目的,这样如果用户 select ALL 那么所有数据都应该显示

数据模型:

    public partial class AuditTable
    {
        public int ID { get; set; }
        public int CompanyId { get; set; }
        public int KeyFieldID { get; set; }
        public System.DateTime DateTimeStamp { get; set; }
        public EntityType DataModel { get; set; }
        public string ValueBefore { get; set; }
        public string ValueAfter { get; set; }
        public string Changes { get; set; }
        public AuditActionType AuditActionTypeENUM { get; set; }

        public int EmployeeId { get; set; }
        public string EmployeeCode { get; set; }
        public string Remarks { get; set; }
        public string IPAddress { get; set; }
        public string UserName { get; set; }
    }
 public enum AuditActionType
    {
        All = 1,
        Create,
        Update,
        Delete
    }

    public enum EntityType
    {
        All = 1,
        BasicDetails,
        EmployeeDetails,
        PersonalDetails                
    }

下面的代码工作正常,但必须重复相同的查询 4 ​​次。我想将以下查询合并为一个

if (eType == EntityType.All)
 if (aType == AuditActionType.All)
  AuditTrail = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.EmployeeCode.Contains(code) && s.UserName.Contains(username)).OrderByDescending(s => s.DateTimeStamp).ToList(); 
 else
  AuditTrail = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.AuditActionTypeENUM == aType && s.EmployeeCode.Contains(code) && s.UserName.Contains(username)).OrderByDescending(s => s.DateTimeStamp).ToList(); 
else
if (aType == AuditActionType.All)
 AuditTrail = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.DataModel == eType && s.EmployeeCode.Contains(code) && s.UserName.Contains(username)).OrderByDescending(s => s.DateTimeStamp).ToList();
else
 AuditTrail = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.AuditActionTypeENUM == aType && s.DataModel == eType && s.EmployeeCode.Contains(code) && s.UserName.Contains(username)).OrderByDescending(s => s.DateTimeStamp).ToList(); 

您可以链接 Where() 个语句。

var baseQuery = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.EmployeeCode.Contains(code) && s.UserName.Contains(username));

var realQuery = baseQuery;

if (eType != EntityType.All){
  realQuery = realQuery.Where(s=>s.DataModel == eType);
}

if (aType != EntityType.All){
  realQuery = realQuery.Where(x=>s.AuditActionTypeENUM == aType);
}

var result = realQuery.OrderByDescending(s => s.DateTimeStamp).ToList()

AuditTrail = result;

全部在一个查询中?

像这样:

bool eTypeIsAll = eType == EntityType.All;
bool aTypeIsAll = aType == AuditActionType.All;
AuditTrail =
    ent.tblAuditTable
       .Where(s => s.KeyFieldID == ID
                   && (
                       eTypeIsAll ? (
                            (
                                !aTypeIsAll ?
                                    s.AuditActionTypeENUM == aType
                                    : true
                            )

                       )
                       : (
                            (
                                !aTypeIsAll ?
                                    s.AuditActionTypeENUM == aType
                                    : true
                            )
                            && s.DataModel == eType

                       )

                   )
                   && s.EmployeeCode.Contains(code)
                   && s.UserName.Contains(username))
        .OrderByDescending(s => s.DateTimeStamp)
        .ToList();

就我个人而言,我更喜欢这种方式。但是很多人说@Jehof方法可读性更好

您应该使用 IQueryables。 无需在一行中编写您的查询。您可以将其传播到多个子查询中。

var queryAble= ent.tblAuditTbale.where(s=>s.KeyFieldId ==Id);

if(eType !=EntityType.All)  
 queryAble = queryAble.where(s=> s.DataModel==eTYpe);

If(aType!= AuditActionType.All)    
 queryAble = queryAble.where(s=> s.AuditActionTypeENUM == aType);

最后(查询将立即执行)

AuditTrail = queryAble.ToList();