Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
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
Posted
Updated 16-Jan-17 21:23pm

Well, in your function declaration you say you return postatus,
but in your code you return a List(Of postatus)
thats what the error message tells you ..

how can you resolve that?

change your code to return a Single postatus ... if your Query only returns 1 element and you a sure of that a .Single will do..
 
Share this answer
 
Comments
CoderWil 16-Jan-17 10:00am    
Thanks, Xaver for your response.

The result contains more than 1 record. I had tried both 'postatus' and 'List(of postatus)' in function declaration but no luck.

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
Additional information: Only parameterless constructors and initializers are supported in LINQ to Entities."
F. Xaver 16-Jan-17 10:23am    
I'm not familiar with EntityFramework :(
maybee change postatus from Structure to Class with an empty Sub New() might help.. but as said, I don't use Entity

but I see 2 options for you
- Change GetPOStatusByLinQ to only return one postatus object
- Or change wherever you use GetPOStatusByLinQ that it support a List(Of postatus) (only if Class idea works)
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):
Quote:
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.
"


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:

VB.NET
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


For further information, please see:
Working with POCO Entities[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900