如果我有以下查询: -
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(根据InvNo列中的值返回一个字符串)并将返回的值放入ChargeType列中,从我读过的方法开始,方法无法翻译成Linq / SQL。
答案 0 :(得分:3)
从我读过的内容来看,方法无法翻译成Linq / SQL。
但您没有使用Linq-To-Sql
,而Linq-To-DataSet
是Linq-To-Objects
的子集。
所以是的,如果没有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)
};
答案 1 :(得分:2)
你试过吗?
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)
};
当您评估查询时(例如ToList()
),CalcChargeType
将被调用/
答案 2 :(得分:2)
由于这不是Linq to SQL,因此您可以毫无问题地调用它。
通过将查询的第一部分强制转换为.AsEnumerable()
,您已将tblCosts
表中的所有数据提取到内存中。现在它不是Linq to SQL查询,而是内存linq查询。
答案 3 :(得分:1)
您必须评估查询(通过调用.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)
};
答案 4 :(得分:1)
你可以尝试:
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)
};
由于您正在使用DataSet扩展而不是Linq-to-SQL,我敢打赌它会起作用。如果您的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)
});