构建动态 linQ 查询
building dynamic linQ queries
我看过一些旧的post但是不知道如何实现这个,请帮忙举个例子。
我需要创建一个筛选器方法以根据表单中的用户输入获取数据,这在本质上可能是随机的。下面给出了我当前的代码,我觉得它很奇怪而且不完整。我如何用动态 LINQ 查询替换它,因为我将来可能需要添加更多参数。我无法弄清楚表达式树的事情。请哪位大神赐教。
public Status GetAssets(String AssetNumber, int AssetCategoryId)
{
var status = Status.ReportInfo("Data Retrieval initiated");
var assetsRepo = new EFRepository<DAL.Entities.Assets, long>(new ProductionControlDataContextFactory(DataBase));
try
{
if (AssetNumber == "" && AssetCategoryId == 0)
{
status = Status.ReportWarning("Invalid Entry.");
}
else if (AssetNumber == "")
{
var assetsData = assetsRepo.FindAll(x => x.AssetCategoryId == AssetCategoryId, x => x.AssetCategory).ToList();
status = Status.ReportSuccess("Data Retrieved Successfully.");
status.Data = assetsData;
}
else if (AssetCategoryId == 0)
{
var assetsData = assetsRepo.FindAll(x => x.AssetNumber == AssetNumber, x => x.AssetCategory).ToList();
status = Status.ReportSuccess("Data Retrieved Successfully.");
status.Data = assetsData;
}
else
{
var assetsData = assetsRepo.FindAll(x => x.AssetNumber == AssetNumber && x.AssetCategoryId == AssetCategoryId, x => x.AssetCategory).ToList();
status = Status.ReportSuccess("Data Retrieved Successfully.");
status.Data = assetsData;
}
}
catch (Exception ex)
{
return Status.ReportError("Couldn't Retrieve Data.");
}
return status;
这是我的实体class
public class Assets : ProductionControlEntity<long>
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public override long Id { get; set; }
[ForeignKey("AssetCategory")]
public int? AssetCategoryId { get; set; }
public virtual AssetCategory AssetCategory { get; set; }
[Index("IX_AssetNumber", 1, IsUnique = true)]
[StringLength(150)]
public string AssetNumber { get; set; }
public string AssetDescription { get; set; }
public string ManufacturerModelNumber { get; set; }
public Boolean CalibrationRequired { get; set; }
public DateTime? LastCalibrationDate { get; set; }
public DateTime? NextCalibrationDueDate { get; set; }
public Boolean AssetStatus { get; set; }
public Boolean IsDeleted { get; set; }
public DateTime LastModified { get; set; }
public string uuid { get; set; }
//public ICollection<EquipmentEntries> EquipmentEntries { get; set; }
}
感谢任何帮助。提前谢谢..
试试这个:
if (string.isNullOrEmpty(AssetNumber) && AssetCategoryId == 0) {
status = Status.ReportWarning("Invalid Entry.");
}
else {
var assetsData = assetsRepo.FindAll(x => ((!string.isNullOrEmpty(AssetNumber) && AssetCategoryId != 0 && x.AssetCategoryId == AssetCategoryId && x.AssetNumber == AssetNumber)
|| (string.isNullOrEmpty(AssetNumber) && AssetCategoryId != 0 && x.AssetCategoryId == AssetCategoryId)
|| (!string.isNullOrEmpty(AssetNumber) && AssetCategoryId == 0 && x.AssetNumber == AssetNumber)), x => x.AssetCategory).ToList();
status = Status.ReportSuccess("Data Retrieved Successfully.");
status.Data = assetsData;
}
如果有任何疑问,请告诉我,祝您编程愉快!!!
我看过一些旧的post但是不知道如何实现这个,请帮忙举个例子。
我需要创建一个筛选器方法以根据表单中的用户输入获取数据,这在本质上可能是随机的。下面给出了我当前的代码,我觉得它很奇怪而且不完整。我如何用动态 LINQ 查询替换它,因为我将来可能需要添加更多参数。我无法弄清楚表达式树的事情。请哪位大神赐教。
public Status GetAssets(String AssetNumber, int AssetCategoryId)
{
var status = Status.ReportInfo("Data Retrieval initiated");
var assetsRepo = new EFRepository<DAL.Entities.Assets, long>(new ProductionControlDataContextFactory(DataBase));
try
{
if (AssetNumber == "" && AssetCategoryId == 0)
{
status = Status.ReportWarning("Invalid Entry.");
}
else if (AssetNumber == "")
{
var assetsData = assetsRepo.FindAll(x => x.AssetCategoryId == AssetCategoryId, x => x.AssetCategory).ToList();
status = Status.ReportSuccess("Data Retrieved Successfully.");
status.Data = assetsData;
}
else if (AssetCategoryId == 0)
{
var assetsData = assetsRepo.FindAll(x => x.AssetNumber == AssetNumber, x => x.AssetCategory).ToList();
status = Status.ReportSuccess("Data Retrieved Successfully.");
status.Data = assetsData;
}
else
{
var assetsData = assetsRepo.FindAll(x => x.AssetNumber == AssetNumber && x.AssetCategoryId == AssetCategoryId, x => x.AssetCategory).ToList();
status = Status.ReportSuccess("Data Retrieved Successfully.");
status.Data = assetsData;
}
}
catch (Exception ex)
{
return Status.ReportError("Couldn't Retrieve Data.");
}
return status;
这是我的实体class
public class Assets : ProductionControlEntity<long>
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public override long Id { get; set; }
[ForeignKey("AssetCategory")]
public int? AssetCategoryId { get; set; }
public virtual AssetCategory AssetCategory { get; set; }
[Index("IX_AssetNumber", 1, IsUnique = true)]
[StringLength(150)]
public string AssetNumber { get; set; }
public string AssetDescription { get; set; }
public string ManufacturerModelNumber { get; set; }
public Boolean CalibrationRequired { get; set; }
public DateTime? LastCalibrationDate { get; set; }
public DateTime? NextCalibrationDueDate { get; set; }
public Boolean AssetStatus { get; set; }
public Boolean IsDeleted { get; set; }
public DateTime LastModified { get; set; }
public string uuid { get; set; }
//public ICollection<EquipmentEntries> EquipmentEntries { get; set; }
}
感谢任何帮助。提前谢谢..
试试这个:
if (string.isNullOrEmpty(AssetNumber) && AssetCategoryId == 0) {
status = Status.ReportWarning("Invalid Entry.");
}
else {
var assetsData = assetsRepo.FindAll(x => ((!string.isNullOrEmpty(AssetNumber) && AssetCategoryId != 0 && x.AssetCategoryId == AssetCategoryId && x.AssetNumber == AssetNumber)
|| (string.isNullOrEmpty(AssetNumber) && AssetCategoryId != 0 && x.AssetCategoryId == AssetCategoryId)
|| (!string.isNullOrEmpty(AssetNumber) && AssetCategoryId == 0 && x.AssetNumber == AssetNumber)), x => x.AssetCategory).ToList();
status = Status.ReportSuccess("Data Retrieved Successfully.");
status.Data = assetsData;
}
如果有任何疑问,请告诉我,祝您编程愉快!!!