简体   繁体   中英

building dynamic linQ queries

I have seen a few old post but cannot figure out how to achieve this, Please help with an example.

I need to create a filter method to get data based on user inputs in a form, which may be random in nature. My current code is given below, which i felt weired and incomplete. How can i replace this with dynamic LINQ queries as the i may need to add more parameters in future. I couldn't figure out the Expression Trees thing. Please somebody enlighten me.

 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;

this is my entity 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; }


}

Any help appreciated. Thanx in advance..

Try this:

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

Let me know if have queries, HAPPY CODING !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM