简体   繁体   English

从Linq查询中调用方法

[英]Call method from within Linq query

If I have the query below :- 如果我有以下查询:-

var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
            group data by new
            {
                InvNo = data.Field<double>("InvoiceNo"),
                AccRef = data.Field<double>("SiteRefNum"),
            }
            into g
            select new
            {
                Code = "1",
                InvType = "I",
                Account = g.Key.AccRef,
                InvNo = g.Key.InvNo,
                ChargeType = ***********
            };

...how I can I call a separate method CalcChargeType (which returns a string, based on the value in the InvNo column) and have the returned value go into column ChargeType, as, from what I've read, methods cannot be translated into Linq/SQL. ...如何调用一个单独的方法CalcChargeType(根据InvNo列中的值返回一个字符串),并将返回的值放入ChargeType列中,因为从我已阅读的内容来看,方法无法转换进入Linq / SQL。

from what I've read, methods cannot be translated into Linq/SQL. 根据我的阅读,方法无法转换为Linq / SQL。

But you are not using Linq-To-Sql but Linq-To-DataSet which is a subset of Linq-To-Objects . 但是您没有使用Linq-To-Sql而是使用Linq-To-DataSet ,它是Linq-To-Objects的子集。

So yes, you can do all that you can do without Linq, fe calling a method: 所以,是的,您可以在不使用Linq的情况下做所有可以做的事情,例如调用方法:

var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
        group data by new
        {
            InvNo = data.Field<double>("InvoiceNo"),
            AccRef = data.Field<double>("SiteRefNum"),
        }
        into g 
        select new
        {
            Code = "1",
            InvType = "I",
            Account = g.Key.AccRef,
            InvNo = g.Key.InvNo,
            ChargeType = CalcChargeType(g.Key.InvNo)
        };

Have you tried ? 你有没有尝试过 ?

var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
            group data by new
            {
                InvNo = data.Field<double>("InvoiceNo"),
                AccRef = data.Field<double>("SiteRefNum"),
            }
            into g
            select new
            {
                Code = "1",
                InvType = "I",
                Account = g.Key.AccRef,
                InvNo = g.Key.InvNo,
                ChargeType = CalcChargeType(g.Key.InvNo)
            };

The, when you will evaluate the query (with ToList() for example), CalcChargeType will be called/ 在评估查询时(例如,使用ToList() ),将调用CalcChargeType /

Since this is not Linq to SQL you may call it without problems. 由于这不是SQL的Linq,因此可以毫无问题地调用它。

By casting the first part of query to .AsEnumerable() you have fetched all data in the tblCosts table to the memory. 通过将查询的第一部分强制转换为.AsEnumerable()您已将tblCosts表中的所有数据提取到内存中。 Now it is not a Linq to SQL query, but an in memory linq query. 现在,它不是Linq to SQL查询,而是内存中的linq查询。

You'd have to evaluate the query (by calling .ToList() ), then re-select and call the function, like so: 您必须评估查询(通过调用.ToList() ),然后重新选择并调用该函数,如下所示:

var Values = (from data in DtSet.Tables["tblCosts"].AsEnumerable()
        group data by new
        {
            InvNo = data.Field<double>("InvoiceNo"),
            AccRef = data.Field<double>("SiteRefNum"),
        }
        into g)
        .ToList()
        .Select(g => new
        {
            Code = "1",
            InvType = "I",
            Account = g.Key.AccRef,
            InvNo = g.Key.InvNo,
            ChargeType = CalcChargeType(g.Key.ChargeType)
        };

Well you could try : 好吧,您可以尝试

var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
        group data by new
        {
            InvNo = data.Field<double>("InvoiceNo"),
            AccRef = data.Field<double>("SiteRefNum"),
        }
        into g
        select new
        {
            Code = "1",
            InvType = "I",
            Account = g.Key.AccRef,
            InvNo = g.Key.InvNo,
            ChargeType = CalcChargeType(g.Key.InvNo)
        };

Since you're using DataSet extensions and not Linq-to-SQL I bet it would work. 由于您使用的是DataSet扩展而不是Linq-to-SQL,所以我敢打赌它会起作用。 If your Linq provider doesn't support it, your next bet is to hydrate what you can and add the ChargeType column afterwards: 如果您的Linq提供程序不支持它,那么您的下一个赌注是充水一下,然后添加ChargeType列:

var Values = (from data in DtSet.Tables["tblCosts"].AsEnumerable()
        group data by new
        {
            InvNo = data.Field<double>("InvoiceNo"),
            AccRef = data.Field<double>("SiteRefNum"),
        }
        into g).ToList() // hydrate the query
        .Select(g=> new
        {
            Code = "1",
            InvType = "I",
            Account = g.Key.AccRef,
            InvNo = g.Key.InvNo,
            ChargeType = CalcChargeType(g.Key.InvNo)
        });

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

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