【问题标题】:building dynamic linQ queries构建动态 linQ 查询
【发布时间】:2018-05-04 08:32:06
【问题描述】:

我看过一些旧帖子,但不知道如何实现这一点,请帮忙举个例子。

我需要创建一个过滤器方法来根据表单中的用户输入获取数据,这在本质上可能是随机的。我当前的代码如下所示,我觉得很奇怪且不完整。我如何用动态 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; }


}

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

【问题讨论】:

  • 如果你投了反对票,请检查更新后的答案,我之前错过了答案中的 not query

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


【解决方案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;
        }

如果有疑问,请告诉我,快乐编码!!!

【讨论】:

  • 已编辑答案,请再次检查,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多