我想在LINQ查询添加Enum
条件即使枚举条件在LINQ查询在C#
我在模型筛选目的而添加ALL
因此如果用户选择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();
不仅另一种是更具可读性,而且还产生较好的SQL查询(与目前的查询提供)。 –
我也一样,但我已经在我的操场上测试了几种不同的方法,并没有发现很大的不同,从查询到查询只有5-10ms ...并且由于您不会自己读取SQL查询,它不会真的不重要。 –
我认为这更多的是关于个人偏好 –