【发布时间】:2017-11-02 09:36:29
【问题描述】:
IQueryable abc = QueryData.Select(a => new { a, TempData = a.customer.Select(b => b.OtherAddress).ToList()[0] }).OrderBy(a => a.TempData).Select(a => aa);
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; }
}
实际数据:
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));
order.Add(new Orders(code + 3, "ANTON", i + 1, 4.3 * i, "NY", "Cholchester", cs));
order.Add(new Orders(code + 4, "BLONP", i + 3, 5.3 * i, "LA", "Marseille", cso));
order.Add(new Orders(code + 5, "BOLID", i + 4, 6.3 * i, "Cochin", "Tsawassen", cs));
public Orders(long OrderId, string CustomerId, int EmployeeId, double Freight, string ShipCountry, string ShipCity, Customer[] Customer = null)
{
this.OrderID = OrderId;
this.CustomerID = CustomerId;
this.EmployeeID = EmployeeId;
this.Freight = Freight;
this.ShipCountry = ShipCountry;
this.ShipCity = ShipCity;
this.customer = Customer;
}
如果我对 OtherAddress 字段第 0 个索引进行排序,则表示仅对 Customer 字段进行排序。我需要根据 OtherAddress 字段对整个订单数据进行排序。
我尝试了以下方法:
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;
}
方法调用:PerformComplexDataOperation(datasource, "customer.0.OtherAddress")
我可以从这一行得到值:var TempData = dataSource.Select(Expression.Lambda>(property, param));
但我无法获得 dataSource.Select(a => new { a, TempData = property});
中的值当我们使用以下代码时它正在工作:
var TempData = dataSource.Select(Expression.Lambda<Func<T, object>>(property, param)).ToList();
IQueryable<object> data = dataSource.Select((a, i) => new { a, TempData = TempData[i] });
这是正确的解决方案吗?
【问题讨论】:
-
为什么? ..........
-
哎呀,您想在不付出任何努力的情况下实现这一点就不能认真吗?了解表达式的工作原理。还有谁在他的正确头脑中支持这样的问题?
-
不确定,但我相信您正在搜索类似 AutoMapper github.com/AutoMapper/AutoMapper
-
嗨@RandRandom 我也需要将这个概念用于其他一些数据,这就是我问的原因
-
你不能在运行时创建匿名类型。但是为什么你需要它,如果你使用像
QueryData.OrderBy(a => a.customer.Select(b => b.OtherAddress).FirstOrDefault())这样没有匿名类型的等效简化版本,我想你应该没有问题动态构建表达式。
标签: c# linq lambda expression