Solved

Help with Linq To Dataset

Posted on 2014-09-17
7
145 Views
Last Modified: 2016-02-15
Hello Experts, I would like to use a Dataset in my application and preform LINQ queries on a datatable rather than the database. I keep running into Queries that I just cannot figure out due to my lack of experience with LINQ. Would someone please show me how to basically to this using a linq query on a datatable.
        Dim strSql2 As String = "SELECT " &
                                "ID," &
                                "iif(groupID=1,""SYSTEM"",iif(groupID=2,""OPTION"",iif(groupID=3,""SPARE"",""RIBBON""))) as [Component]," &
                                "Qty," &
                                "PartNumber as [Part Number]," &
                                "Description," &
                                "IDTCost as [IDT Cost]," &
                                "ListPrice as [List Price]," &
                                "Discount," &
                                "(1-Discount) * ListPrice AS [Sell Each]," &
                                "Qty * IDTCost AS [Ext Cost]," &
                                "Qty * ((1-Discount) * ListPrice) as [Ext Sell]," &
                                "1 - ((Qty * IDTCost) / (Qty * ((1-discount) * ListPrice))) as [Margin] " &
                                "FROM ProjectItems " &
                                "WHERE ProjectID=" & projectID & " " &
                                "ORDER BY groupID ASC"

Open in new window

0
Comment
Question by:Basicfarmer
  • 4
  • 3
7 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40329440
So you are going to execute the above SQL query and fill the DataSet using ADO .Net and then you will execute the Linq query against the DataSet / DataTable? What do you want to return from the DataSet?
0
 

Author Comment

by:Basicfarmer
ID: 40329477
My plan is to access the database one time when the app is started and fill a dataset with all the tables that i will need in the application. Then I want to use linq to query those datatables. In my eample above I want to fill a datatable with like this.

Dim strSql as string = "SELECT * FROM ProjectItems"
Dim dt as new DataTable
Dim da as new oleDbDataAdapter(strSql,conn)
da.fill(dt)

Now the query in my example above is what I used to do to get the information I needed. But now all the information will be in the DataTable and I want to get the same information as in the example using Linq.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40329517
Hi Basicfarmer;

Something like this should work. I am not sure about the data types of the columns so make sure that the code in Field(Of XXX) has the correct data type for the column.

Dim projectID As Integer = Value to use in the Where clause                      
Dim results = (From items in dt.AsEnumerable() _
               Where item.Field(Of Integer)("ProjectID") = projectID _
               Order By item.Field(Of Integer)("groupID") _
               select ID = item.Field(Of Integer)("ID"), _
                      Component = item.Field(Of String)("Component"), _
                      Qty = item.Field(Of String)("Qty"), _
                      PartNumber = item.Field(Of Integer)("Part Number"), _
                      Description = item.Field(Of Integer)("Description"), _
                      
                      '' Keep following this template for the rest of the columns
                      
                      Margin = item.Field(Of Double)("Margin") _
               )

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Basicfarmer
ID: 40329521
I will focus on this tomorrow when I get to the Office. Thanks for the quick response...
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40329533
Not a problem, glad to help.
0
 

Author Closing Comment

by:Basicfarmer
ID: 40333383
Fernando, that you for this post. It has given me a very good starting point.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40333394
Not a problem Basicfarmer, glad to help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now