Linq自定义数据错误 [英] Linq to a custom data error
问题描述
我尝试将一组新数据添加到预定义的数据字典中但是我在Return Result.tolist()中遇到错误消息
错误BC30311List(Of S_pocontRepo.postatus)类型的值无法转换为S_pocontRepo.postatus。
I try to get a new set of data into a predefined data dictionary however I encounter error message at the "Return Result.tolist()"
"Error BC30311 Value of type 'List(Of S_pocontRepo.postatus)' cannot be converted to 'S_pocontRepo.postatus'."
Structure postatus
Public po_no As String
Public po_date As String
Public crcode As String
Public name As String
Public qty As Decimal
Public qty_rec As Decimal
Public os_qty As Decimal
Public dt_due As DateTime
Public status As String
Public rowid As Integer
End Structure
''Note
''The EXT.NET Grid Store cannot accept EF List as datasource so we need to use LindQ.
''
Public Function GetPOStatusByLinQ(ByVal inPartcode As String) As postatus '' List(Of postatus)
''LinQ
''-----------------------------------------------------------------------------------------------
Dim result = From p In Db.s_pocont
Group Join c In Db.s_apmaster On c.crcode Equals p.crcode Into podtl_ap = Group
From c In podtl_ap.DefaultIfEmpty()
Group Join h In Db.s_pohdr On h.po_no Equals p.po_no Into podtl_hdr = Group
From h In podtl_hdr.DefaultIfEmpty()
Where p.part_code = inPartcode And
h.status <> "CANCELLED" And
h.status <> "COMPLETED" And
p.type <> "D" And
p.os_qty > 0
Select New postatus With {
.po_no = p.po_no,
.po_date = h.date,
.crcode = p.crcode,
.name = c.name,
.qty = p.qty,
.qty_rec = p.qty_rec,
.os_qty = p.os_qty,
.dt_due = p.dt_due,
.status = h.status,
.rowid = p.rowid
}
Return result.ToList()
End Function
我的尝试:
我注意到
What I have tried:
I notice
IEnumerable
数据类型无法转换为VB.net中的List< of,这就是我尝试使用上述方法的原因。但是,到了死胡同。
如何解决这个问题?
TIA
data type is not able to convert to "List<of" in VB.net and that's why I tried to work around with above method. yet, hitting dead end.
How can I resolve this?
TIA
推荐答案
好吧,在你的函数声明中你说你返回postatus
,
但是在你的代码中你返回一个列表(Postatus)
这就是错误信息告诉你的内容..
你怎么解决这个问题?
更改您的代码以返回单个postatus
...如果您的查询只返回1个元素而您确定.Single
会做..
Well, in your function declaration you say you returnpostatus
,
but in your code you return aList(Of postatus)
thats what the error message tells you ..
how can you resolve that?
change your code to return a Singlepostatus
... if your Query only returns 1 element and you a sure of that a.Single
will do..
我建议你读一下:类型'< type1>'的值无法转换为'< type2>' [ ^ ]
它可能有助于您理解你做错了什么。
关于你的另一个问题(在评论解决方案#1):
I'd suggest to read this: Value of type '<type1>' cannot be converted to '<type2>'[^]
It might help you to understand what you're doing wrong.
As to your another issue (in the comment to the solution #1):
当我改为列表( of postatus)
,错误消息显示
When I change to List(of postatus)
, the error message shows "
An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code<br />
Additional information: Only parameterless constructors and initializers are supported in LINQ to Entities.
此错误当Entity Framework不知道如何将Linq Select()
语句转换为SQL表达式时,将显示消息。如果您需要对不是实体的POCO进行数据转换,您应首先从EF获取相关数据,然后将其转换为POCO,例如:
This error message is displayed when Entity Framework doesn't know how to translate Linq Select()
statement into SQL expression. If you need to do a data transformation to a POCO that is not an entity, you should first get the relevant data from EF and then transform it to the POCO, for example:
Public Function GetPOStatusByLinQ(ByVal inPartcode As String) As List(Of postatus)
''LinQ
''-----------------------------------------------------------------------------------------------
Dim EfResult = (From p In Db.s_pocont
Group Join c In Db.s_apmaster On c.crcode Equals p.crcode Into podtl_ap = Group
From c In podtl_ap.DefaultIfEmpty()
Group Join h In Db.s_pohdr On h.po_no Equals p.po_no Into podtl_hdr = Group
From h In podtl_hdr.DefaultIfEmpty()
Where p.part_code = inPartcode And
h.status <> "CANCELLED" And
h.status <> "COMPLETED" And
p.type <> "D" And
p.os_qty > 0) _
.ToList() 'this causes the query to execute
'now, you can return POCO model
Dim PocoResult = (From efr In EfResult
Select New postatus With {
.po_no = p.po_no,
.po_date = h.date,
.crcode = p.crcode,
.name = c.name,
.qty = p.qty,
.qty_rec = p.qty_rec,
.os_qty = p.os_qty,
.dt_due = p.dt_due,
.status = h.status,
.rowid = p.rowid
}) _
.ToList()
Return PocoResult
End Function
这篇关于Linq自定义数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!