所以我正在尝试制作动态报告,我们的用户可以保存并稍后参考。我决定使用linq,并学习更多关于它的内容。我觉得我有很多这个想法,但最后却被困住了。这是我的代码
protected void executeReport(CustomerCustomReports report)
{
string fields = report.fields;
ReportsDataContext db = new ReportsDataContext();
string[]filters = fields.Split(';');
var inventoryList = (from cust in db.Customers
join product in db.Products on cust.customerID equals product.customerID
join inventory in db.Inventories on product.itemID equals inventory.ItemId
join warehouse in db.Warehouses on inventory.WarehouseId equals warehouse.Id
where cust.customerID == customer.CustomerId
select new
{
ItemID = product.itemID,
ItemDescription = product.shortDesc,
CustomsValue = product.unitPrice
}).OrderBy(k => k.ItemID);
ParameterExpression sourceItem = Expression.Parameter(inventoryList.ElementType, "x");
Type resultType = typeof(linqResult);
var dynamicFields = new List<MemberBinding>();
foreach (string f in filters)
{
if (!String.IsNullOrEmpty(f))
{
dynamicFields.Add(Expression.Bind(resultType.GetMember(f)[0], Expression.Property(sourceItem, inventoryList.ElementType.GetProperty(f))));
}
}
Expression selector = Expression.Lambda(Expression.MemberInit(Expression.New(resultType.GetConstructor(Type.EmptyTypes)), dynamicFields), sourceItem);
var query = inventoryList.Provider.CreateQuery(
Expression.Call(
typeof(Queryable),
"Select",
new Type[] { inventoryList.ElementType, resultType },
Expression.Constant(inventoryList), selector));
var listResult = new List<object>();
var enumerator = query.GetEnumerator();
while (enumerator.MoveNext())
{
listResult.Add(enumerator.Current);
}
gvResults.DataSource = listResult;
gvResults.DataBind();
}
有这样的课程
public class linqResult
{
public string ItemID {get; set;}
public string ItemDescription {get; set;}
public string CustomsValue {get; set;}
}
我的目标是拥有完整列表,然后只显示客户想要的属性。
答案 0 :(得分:0)
List<LinqResult> inventoryList = (from cust in db.Customers
join product in db.Products on cust.customerID equals product.customerID
join inventory in db.Inventories on product.itemID equals inventory.ItemId
join warehouse in db.Warehouses on inventory.WarehouseId equals warehouse.Id
where cust.customerID == customer.CustomerId
select cust).OrderBy(k => k.ItemID).ToList();
这有用吗?我在网站上打了它,但是jist只是从查询中获取你的列表集合,然后在你得到它时将它转换成你的LinqResult。您可能需要将其强制转换为ToList - 但请使用它,看看它为您做了什么。