构建动态linQ查询

时间:2018-05-04 08:32:07

标签: c# linq entity-framework-6 expression-trees dynamicquery

我看过一些旧帖子,但无法弄清楚如何实现这一点,请帮助举个例子。

我需要创建一个过滤方法来根据表单中的用户输入获取数据,这可能是随机的。我目前的代码如下所示,我感到厌倦和不完整。我如何用动态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;

这是我的实体类

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; }


}

任何帮助表示赞赏。 Thanx提前..

1 个答案:

答案 0 :(得分:-1)

试试这个:

 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;
        }

如果有疑问,请告诉我,HAPPY CODING !!!