none
How to build () => new { x.prop} lambda expression dynamically? RRS feed

  • Question

  • How to dynamically create the below linq expression.

    IQueryable abc = QueryData.Select(a => new { a, TempData = a.customer.Select(b => b.OtherAddress).ToList()[0] }).OrderBy(a => a.TempData).Select(a => a.a);

    I have tried the below way:
    private static IQueryable PerformComplexDataOperation<T>(this IQueryable<T> dataSource, string select)
        {
            string[] selectArr = select.Split('.');
            ParameterExpression param = Expression.Parameter(typeof(T), "a");
            Expression property = param;
            for (int i = 0; i < selectArr.Length; i++)
            {
                int n;
                if (int.TryParse(selectArr[i + 1], out n))
                {
                    int index = Convert.ToInt16(selectArr[i + 1]);
                    property = Expression.PropertyOrField(Expression.ArrayIndex(Expression.PropertyOrField(property, selectArr[i]), Expression.Constant(index)), selectArr[i + 2]);
                    i = i + 2;
                }
                else property = Expression.PropertyOrField(property, selectArr[i]);
            }
            var TempData = dataSource.Select(Expression.Lambda<Func<T, object>>(property, param));
    
           IQueryable<object> data = dataSource.Select(a => new { a, TempData = property});// Expression.Lambda<Func<T, object>>(property, param) });
            return data;
        }

    Method call : PerformComplexDataOperation(datasource, "customer.0.OtherAddress")

    I can get the value from this line : var TempData = dataSource.Select(Expression.Lambda>(property, param));

    But i can't get the values in dataSource.Select(a => new { a, TempData = property});

    Tuesday, November 7, 2017 9:36 AM

Answers

All replies

  • public class Orders
    {
        public long OrderID { get; set; }
        public string CustomerID { get; set; }
        public int EmployeeID { get; set; }
        public double Freight { get; set; }
        public string ShipCountry { get; set; }
        public string ShipCity { get; set; }
    
        public Customer[] customer {get; set;}
    }
    
    public class Customer
    {
        public string OtherAddress { get; set; }
        public int CustNum { get; set; }
    }


    Tuesday, November 7, 2017 9:38 AM
  • Actual data:

    List<Orders> order = new List<Orders>();
    Customer[] cs = { new Customer { CustNum = 5, OtherAddress = "Hello" }, new 
    Customer { CustNum = 986, OtherAddress = "Other" } };
    Customer[] cso = { new Customer { OtherAddress = "T", CustNum = 5 }, new 
    Customer { CustNum = 777, OtherAddress = "other" } };
    order.Add(new Orders(code + 1, "ALFKI", i + 0, 2.3 * i, "Mumbari", "Berlin", cs));
    order.Add(new Orders(code + 2, "ANATR", i + 2, 3.3 * i, "Sydney", "Madrid", cso));
    
    Tuesday, November 7, 2017 9:40 AM
  • If i sort the OtherAddress field 0th index means Customer field only sorted. I need to sort the whole order data based on OtherAddress field.
    Tuesday, November 7, 2017 9:41 AM
  • I'd try them over here.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=vslanguages

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Tuesday, November 7, 2017 1:19 PM
    Moderator