Scott Harrison - The golden apples
Henry Parker's Robot Wars

ASP.Net, ADO, MS Access
C# How to dynamically expand our linq where clause

C# How to dynamically expand our linq where clause

(4 hits)
Overview: Hits=5 This code snippet demonstrates how to use IQueryable to dynamically expand the linq expression tree. The where clause appends to the expression tree. The IQueryable is not evaluated and executed until the foreach. The MapChartOfAccountView flattens the data into a business view.

IQueryable

public List<ChartOfAccountView> GetChartOfAccountViewsByAccount(string companyNumber, string busUnit, string objectNumber, string subsidiary)
        {
            try
            {

                IQueryable<ChartOfAccts> query = _dbContext.ChartOfAccts;

                if (companyNumber != "")
                {
                    query = query.Where(e => e.CompanyNumber == companyNumber);
                }
                if (busUnit != "")
                {
                    query = query.Where(e => e.BusUnit == busUnit);
                }
                if (objectNumber != "")
                {
                    query = query.Where(e => e.ObjectNumber == objectNumber);
                }
                if (subsidiary != "")
                {
                    query = query.Where(e => e.Subsidiary == subsidiary);
                }
                List<ChartOfAccountView> list = new List<ChartOfAccountView>();
                foreach (var item in query)
                {
                    list.Add
                        (
                          applicationViewFactory.MapChartOfAccountView(item)
                        );

                }
                return list;
            }
            catch (Exception ex) { throw new Exception(GetMyMethodName(), ex); }

        }

MapChartOfAccountView

  public override ChartOfAccountView MapChartOfAccountView(ChartOfAccts chartOfAcct)
        {
            return new ChartOfAccountView(chartOfAcct);
        }

 public class ChartOfAccountView
    {
        public ChartOfAccountView() { }
        public ChartOfAccountView(ChartOfAccts chartOfAcct)
        {
            this.AccountId = chartOfAcct.AccountId;
            this.Location = chartOfAcct.Location;
            this.BusUnit = chartOfAcct.BusUnit;
            this.Subsidiary = chartOfAcct.Subsidiary;
            this.SubSub = chartOfAcct.SubSub;
            this.Account = chartOfAcct.Account;
            this.Description = chartOfAcct.Description;
            this.CompanyNumber = chartOfAcct.CompanyNumber;
            this.GenCode = chartOfAcct.GenCode;
            this.SubCode = chartOfAcct.SubCode;
            this.ObjectNumber = chartOfAcct.ObjectNumber;
            this.SupCode = chartOfAcct.SupCode;
            this.ThirdAccount = chartOfAcct.ThirdAccount;
            this.CategoryCode1 = chartOfAcct.CategoryCode1;
            this.CategoryCode2 = chartOfAcct.CategoryCode2;
            this.CategoryCode3 = chartOfAcct.CategoryCode3;
            this.PostEditCode = chartOfAcct.PostEditCode;
            this.CompanyId = chartOfAcct.CompanyId;
            this.CompanyName = chartOfAcct.Company?.CompanyName;
            this.Level = chartOfAcct.Level;
        }
        public long AccountId { get; set; }
        public string Location { get; set; }
        public string BusUnit { get; set; }
        public string Subsidiary { get; set; }
        public string SubSub { get; set; }
        public string Account { get; set; }
        public string Description { get; set; }
        public string CompanyNumber { get; set; }
        public string GenCode { get; set; }
        public string SubCode { get; set; }
        public string ObjectNumber { get; set; }
        public string SupCode { get; set; }
        public string ThirdAccount { get; set; }
        public string CategoryCode1 { get; set; }
        public string CategoryCode2 { get; set; }
        public string CategoryCode3 { get; set; }
        public string PostEditCode { get; set; }
        public long CompanyId { get; set; }
        public string CompanyName { get; set; }
        public int Level { get; set; }
    }
....

...<<<Register to correspond>>> ...

Members : 152
Name:
Email:

Register to View

Help