将 VB Linq with Group by 和 Select 转换为 C#
Convert VB Linq with Group by and Select to C#
我正在尝试将以下 LINQ 代码从 VB.NET 转换为 C#,这是在数据表“tvp_dtl”上执行的。后跟VB代码。
Dim query
query = From row In tvp_dtl
Group row By dataGroup = New With {
Key .Tmall_txn_no = row.Field(Of String)("Tmall_txn_no"),
Key .stock_code = row.Field(Of String)("stock_code"),
Key .ItemNameLocalLang = row.Field(Of String)("ItemNameLocalLang"),
Key .ItemUnitPrice = row.Field(Of String)("ItemUnitPrice"),
Key .itemTotalAmount = row.Field(Of String)("itemTotalAmount")
} Into Group
Select New With {
.Tmall_txn_no = dataGroup.Tmall_txn_no,
.stock_code = dataGroup.stock_code,
.ItemNameLocalLang = dataGroup.ItemNameLocalLang,
.ItemUnitPrice = dataGroup.ItemUnitPrice,
.itemTotalAmount = dataGroup.itemTotalAmount,
.SumAmount = Group.Sum(Function(x) Integer.Parse(x.Field(Of String)("ItemNumber")))}
这里是我用C#写的版本
var query = from row in tvp_dtl
group row by new
{
row.Tmall_txn_no = row.Field<string>("Tmall_txn_no"),
row.stock_code = row.Field<string>("stock_code"),
row.ItemNameLocalLang = row.Field<string>("ItemNameLocalLang")
} into dataGroup
select new
{
Tmall_txn_no = dataGroup.Field<string>("Tmall_txn_no"),
stock_code = dataGroup.Field<string>("stock_code"),
ItemNameLocalLang = dataGroup.Field<string>("ItemNameLocalLang"),
ItemUnitPrice = dataGroup.Field<string>("ItemUnitPrice"),
itemTotalAmount = dataGroup.Field<string>("itemTotalAmount"),
SumAmount = dataGroup.Sum(x => int.Parse(x.Field<string>("ItemNumber") * int.Parse(x.Field<string>("QtyPerSet"))))
};
但我认为这是不正确的。此外,我在使用“tvp_dtl”
时遇到此错误
Could not find the implementation of the query pattern for source
type "DataTable". Group By not found
DataTable
不是 IEnumerable
,更糟糕的是,它不提供编译器要求的方法来充当可枚举对象。
public class DataTable : MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISupportInitialize, ISerializable, IXmlSerializable
幸运的是,有一种扩展方法可以解决这个问题。
试试这个:
var query =
from row in tvp_dtl.AsEnumerable()
我正在尝试将以下 LINQ 代码从 VB.NET 转换为 C#,这是在数据表“tvp_dtl”上执行的。后跟VB代码。
Dim query
query = From row In tvp_dtl
Group row By dataGroup = New With {
Key .Tmall_txn_no = row.Field(Of String)("Tmall_txn_no"),
Key .stock_code = row.Field(Of String)("stock_code"),
Key .ItemNameLocalLang = row.Field(Of String)("ItemNameLocalLang"),
Key .ItemUnitPrice = row.Field(Of String)("ItemUnitPrice"),
Key .itemTotalAmount = row.Field(Of String)("itemTotalAmount")
} Into Group
Select New With {
.Tmall_txn_no = dataGroup.Tmall_txn_no,
.stock_code = dataGroup.stock_code,
.ItemNameLocalLang = dataGroup.ItemNameLocalLang,
.ItemUnitPrice = dataGroup.ItemUnitPrice,
.itemTotalAmount = dataGroup.itemTotalAmount,
.SumAmount = Group.Sum(Function(x) Integer.Parse(x.Field(Of String)("ItemNumber")))}
这里是我用C#写的版本
var query = from row in tvp_dtl
group row by new
{
row.Tmall_txn_no = row.Field<string>("Tmall_txn_no"),
row.stock_code = row.Field<string>("stock_code"),
row.ItemNameLocalLang = row.Field<string>("ItemNameLocalLang")
} into dataGroup
select new
{
Tmall_txn_no = dataGroup.Field<string>("Tmall_txn_no"),
stock_code = dataGroup.Field<string>("stock_code"),
ItemNameLocalLang = dataGroup.Field<string>("ItemNameLocalLang"),
ItemUnitPrice = dataGroup.Field<string>("ItemUnitPrice"),
itemTotalAmount = dataGroup.Field<string>("itemTotalAmount"),
SumAmount = dataGroup.Sum(x => int.Parse(x.Field<string>("ItemNumber") * int.Parse(x.Field<string>("QtyPerSet"))))
};
但我认为这是不正确的。此外,我在使用“tvp_dtl”
时遇到此错误Could not find the implementation of the query pattern for source type "DataTable". Group By not found
DataTable
不是 IEnumerable
,更糟糕的是,它不提供编译器要求的方法来充当可枚举对象。
public class DataTable : MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISupportInitialize, ISerializable, IXmlSerializable
幸运的是,有一种扩展方法可以解决这个问题。
试试这个:
var query =
from row in tvp_dtl.AsEnumerable()