return LINQ results and bind to gridview - VB.NET

Hello EE!

Trying to query a datatable using LINQ to enhance my application.  I'm able to get the query working just fine but want to return it as a list and further bind it to a gridview.








    

Public Class Employee

_code as string
_title as string
_min as decimal

public sub new(byval c as string, byval t as string, byval m as decimal)

Me.Code = c
Me.Title = t
Me.SalaryMin = m

end sub

End Class

'my function to return the query results
Protected Function rtnLINQ() As IQueryable(Of Employee)

        Dim ds As New DataSet
        ds = DirectCast(Session("oDS"), DataSet)

        Dim dtGroups As DataTable = ds.Tables(0)

        Dim qLINQ As List(Of Employee) = From group In dtGroups.AsEnumerable() Where _
                    group.Field(Of Integer)("POS_ID") = 1321 _
        Select New Employee With { _
        .JobCode = group.Field(Of String)("JOB_CODE"), _
        .JobTitle = group.Field(Of String)("JOB_TITLE"), _
        .SalaryMin = group.Field(Of Decimal)("SALARY_MIN") _
        }

        Return qLINQ

End Function


Public Sub GetMyFilter()

gv.datasource = rtnLINQ()
gv.databind()

End Sub

Open in new window

sk1922Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

markmiddlemistCommented:
What you've got there should work. What's not doing what you expect?
0
käµfm³d 👽Commented:
You don't have to return the object as a List in order to databind it. Usually, you can assign the Linq query itself as the DataSource.

I do see that you've got a bit of implicit conversion attempting to go on. My suggestion would be to change the function's return type from IQueryable to IEnumerable and use a type-less Dim for your Linq query reference variable. In other words:

Protected Function rtnLINQ() As IEnumerable(Of Employee)

        Dim ds As New DataSet

        ds = DirectCast(Session("oDS"), DataSet)

        Dim dtGroups As DataTable = ds.Tables(0)

        Dim qLINQ = From group In dtGroups.AsEnumerable() _
                    Where group.Field(Of Integer)("POS_ID") = 1321 _
                    Select New Employee With _
                           { _
                               .JobCode = group.Field(Of String)("JOB_CODE"), _
                               .JobTitle = group.Field(Of String)("JOB_TITLE"), _
                               .SalaryMin = group.Field(Of Decimal)("SALARY_MIN") _
                           }

        Return qLINQ

End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sk1922Author Commented:
Thank you both for the response.  

Before and even after changing from IQueryable to IEnumberable, I get this error:


Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30455: Argument not specified for parameter 'code' of 'Public Sub New(code As String, title As String, salMin As Decimal)'.

Source Error:

Line 46:         Dim qLINQ = From group In dtGroups.AsEnumerable() Where _
Line 47:                     group.Field(Of Integer)("POS_ID") = 1321 _
Line 48:         Select New Employee With {.JobCode = group.Field(Of String)("JOB_CODE"), .JobTitle = group.Field(Of String)("JOB_TITLE"), .SalaryMin = group.Field(Of Decimal)("SALARY_MIN")}
Line 49:
Line 50:         Return qLINQ
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

CodeCruiserCommented:
Do you have a parameterless constructor on the employee class?
0
sk1922Author Commented:
Ahhhhh haaaaa!!! That's was it. Awesome!


Now, in your comment above, you mentioned "usually you can bind the LINQ results as a DataSource..."  in what instances (high-level) do you typically need to convert the results into a list?  I've seen, plastered all over the web, examples that use .ToList()... any words of wisdom you can offer up on this?  I just want to get some understanding before I go off trying to LINQ-ify some of my applications.... :) fun times.
0
markmiddlemistCommented:
The way your code is set up the linq query is trying to create the Employee class with the parameterless constructor as CodeCruiser says, then populate each of the parameters in the With claue. As your constructor is actually expecting each of the values, what you should actually do if you don't want to change the constructor is pass the values in as parameters to the constructor as so:

Line 46:         Dim qLINQ = From group In dtGroups.AsEnumerable() Where _
Line 47:                     group.Field(Of Integer)("POS_ID") = 1321 _
Line 48:         Select New Employee(group.Field(Of String)("JOB_CODE"), group.Field(Of String)("JOB_TITLE"), group.Field(Of Decimal)("SALARY_MIN"))
Line 49:
Line 50:         Return qLINQ
0
markmiddlemistCommented:
The IEnumerable is fine for your databinding here. I think you'll find that most of the examples with .ToList are when using Ling2Sql or EntityFramework, where the ToList is often used to force actual execution of the generated SQL and loading of the data in to your classes, so thats not going to be required in your model.
0
käµfm³d 👽Commented:
I think you'll find that most of the examples with .ToList are when using Ling2Sql or EntityFramework, where the ToList is often used to force actual execution of the generated SQL and loading of the data in to your classes, so thats not going to be required in your model.
Agreed. But I believe in databinding the query itself you get the same effect as calling ToList. I'd have to test it out though.
0
sk1922Author Commented:
Thank you for the clarification and the code... really helped.  I'm on my way.... slowly but surely :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.