简体   繁体   English

如何在动态linq查询中获取匿名字段的总和

[英]how to get sum of anonymous field in dynamic linq query

to show sum of field in footer row of gridview i want to get sum from my dynamic list, InTopSuppliersView() is in separate assembly DAL while gridview is inside presentation layer. 为了在gridview的页脚行中显示字段总和,我想从我的动态列表中获取总和,InTopSuppliersView()在单独的程序集DAL中,而gridview在表示层中。

Code in DAL: DAL中的代码:

public static dynamic InTopSuppliersView()
{

    var _pd = from pd in InTopSuppliers()
                join pm in PdnmasterDALC.InTopSuppliers() on pd.Pdnmasterid equals pm.Pdnmasterid
                select new
                {
                    RefNo = pm.PDNRefNo,
                    Date = pm.Date
                    Quantity = pd.QuantityApprovedUOM,
                    Rate = pd.Rate,
                    Amount = pd.Amount
                };

    return _pd;
}

Code on form: 表格上的代码:

var tmp = InTopSuppliersView();
gvPDNDetail.DataSource = tmp;
gvPDNDetail.DataBind();

gvPDNDetail.FooterRow.Cells[3].Text = tmp.SUM(o => o.Quantity).ToString();

i want to get sum of fields Quantity and Amount created dynamically in query 我想获取查询中动态创建的字段“数量”和“金额”的总和

在此处输入图片说明

Let's see if I understood you correctly: 让我们看看我是否正确理解了您:


You return a dynamic from InTopSuppliersView because you want to return a list with anonymous type, right? 您从InTopSuppliersView返回dynamic ,因为您想返回带有匿名类型的列表,对吗? And then you want to call .Sum on that list, but it won't work with the syntax because dynamic doesn't know what a lambda expression is ( o => o.Quantity ): 然后,您想在该列表上调用.Sum ,但是它不适用于语法,因为dynamic不知道什么是lambda表达式( o => o.Quantity ):

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type 在未先将lambda表达式转换为委托或表达式树类型之前,不能将lambda表达式用作动态调度操作的参数

So I'm guessing you want to do something like this: 所以我猜你想做这样的事情:

gvPDNDetail.FooterRow.Cells[3].Text = ((IEnumerable<dynamic>)tmp).Sum(o => o.Quantity).ToString();

Or just change the return type to IEnumerable<dynamic> , like so: 或者只是将返回类型更改为IEnumerable<dynamic> ,如下所示:

public static IEnumerable<dynamic> InTopSuppliersView()
{
    return from pd in InTopSuppliers()
           join pm in PdnmasterDALC.InTopSuppliers() on pd.Pdnmasterid equals pm.Pdnmasterid
           select new
           {
               RefNo = pm.PDNRefNo,
               Date = pm.Date
               Quantity = pd.QuantityApprovedUOM,
               Rate = pd.Rate,
               Amount = pd.Amount
           };
}

Then call it like before: 然后像以前一样调用它:

// Observe the casing on Sum
gvPDNDetail.FooterRow.Cells[3].Text = tmp.Sum(o => o.Quantity).ToString();

EDIT: 编辑:

From your comments I think I know what's happening. 从您的评论中,我想我知道发生了什么事。 You have the InTopSuppliersView method in one assembly and the form where you use it in another. 在一个程序InTopSuppliersViewInTopSuppliersView方法,在另一个程序InTopSuppliersView有您使用它的窗体。 I believe the anonymous type created is by default internal and thus cannot be used in another assembly. 我相信默认情况下创建的匿名类型是internal ,因此不能在另一个程序集中使用。 I think it will work if you use ExpandoObject instead: 我认为如果您使用ExpandoObject相反,它将起作用:

public static IEnumerable<dynamic> InTopSuppliersView()
{
    var _pd = from pd in InTopSuppliers()
              join pm in PdnmasterDALC.InTopSuppliers() on pd.Pdnmasterid equals pm.Pdnmasterid
              select new { pd, pm };

    return _pd.Select(o =>
    {
        dynamic obj = new ExpandoObject();

        obj.RefNo = o.pm.PDNRefNo;
        obj.Date = o.pm.Date;
        obj.Quantity = o.pd.QuantityApprovedUOM;
        obj.Rate = o.pd.Rate;
        obj.Amount = o.pd.Amount;

        return obj;
    });
}

You could also (instead of above) use [assembly: InternalsVisibleTo("YourAssemblyName")] in the project where this method is and change "YourAssemblyName" with the assembly name of the forms project. 您也可以(而不是上面的方法)在此方法所在的项目中使用[assembly: InternalsVisibleTo("YourAssemblyName")] ,并使用表单项目的程序集名称更改“ YourAssemblyName”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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