xml的多組閱讀有關LINQ很多帖子,以XML後,許多試驗採用分組我現在已經格式化以下LINQ查詢:的LINQ to by子句
Dim objList = (From i In xmlDoc.Descendants("payment_amnt")
Group CType(i.Value, Decimal)
By Key = i.Parent.Element("account_id").Value.ToString() Into g = Group
Select New With {.ItemName = Key, .Sum = g.Sum()}).ToList()
我想補充另一組鍵(「payment_mnth 「)但當我這樣做:
By Key = i.Parent.Element("account_id").Value.ToString(), Key = i.Parent.Element("payment_mnth").Value.ToString() Into g = Group
我得到的錯誤。
如何添加第二組鍵來此查詢?
在此先感謝。
斯蒂芬
PS:大衛反饋後,我已經添加了以下內容linqpad和得到這個錯誤:對象引用不設置到對象的實例。
Dim xmlDoc As XDocument = XDocument.Load("c:\\Backup\PAYMENT.xml")
Dim objList = (From i In xmlDoc.Descendants("payment_amnt")
Group CType(i.Value, Decimal)
By AccountId = i.Parent.Element("account_id").Value.ToString(), PaymentMonth = i.Parent.Element("payment_mnth").Value.ToString() Into g = Group
Select New With {AccountId, PaymentMonth, g.Sum()}).ToList()
objList.Dump()
它似乎是'By'部分中的AccountId導致錯誤。我的XML如下:
在你嘗試<?xml version="1.0" encoding="utf-8"?>
<PAYMENT xmlns="urn:lst-emp:emp">
<PAYMENT xmlns="">
<id>1</id>
<paymentdate>17/06/2013</paymentdate>
<payment_no>1</payment_no>
<payment_to_id>1</payment_to_id>
<PAYMENT_TO>PWA</PAYMENT_TO>
<payment_type_id>1</payment_type_id>
<PAYMENT_TYPE>Computer Services</PAYMENT_TYPE>
<payment_request>Stephen</payment_request>
<payment_amnt>100</payment_amnt>
<comment></comment>
<payment_checked>0</payment_checked>
<GST>9.09</GST>
<payment_month>Jun</payment_month>
<account_id_transfer />
<account_id>1</account_id>
</PAYMENT>
<PAYMENT xmlns="">
<id>2</id>
<paymentdate>17/06/2013</paymentdate>
<payment_no>2</payment_no>
<payment_to_id>1</payment_to_id>
<PAYMENT_TO>PWA</PAYMENT_TO>
<payment_type_id>1</payment_type_id>
<PAYMENT_TYPE>Computer Services</PAYMENT_TYPE>
<payment_request>Stephens</payment_request>
<payment_amnt>1000</payment_amnt>
<comment></comment>
<payment_checked>0</payment_checked>
<GST>90.91</GST>
<payment_month>Jun</payment_month>
<account_id_transfer />
<account_id>1</account_id>
</PAYMENT>
<PAYMENT xmlns="">
<id>3</id>
<paymentdate>17/06/2013</paymentdate>
<payment_no>3</payment_no>
<payment_to_id>1</payment_to_id>
<PAYMENT_TO>PWA</PAYMENT_TO>
<payment_type_id>1</payment_type_id>
<PAYMENT_TYPE>Computer Services</PAYMENT_TYPE>
<payment_request>Stephen</payment_request>
<payment_amnt>145</payment_amnt>
<comment></comment>
<payment_checked>0</payment_checked>
<GST>13.18</GST>
<payment_month>Jun</payment_month>
<account_id_transfer />
<account_id>1</account_id>
</PAYMENT>
</PAYMENT>
感謝糾正我的複製和粘貼錯誤。我試過,但我得到了錯誤:範圍變量名稱不能匹配「對象」類的成員的名字。 –
我已經更新我的崗位回答您的評論。 –
使用LinqPad我創建了一個新的查詢,它仍然錯誤。我在上面添加了查詢和XML。 –