Solved

Help with Linq To Dataset

Posted on 2014-09-17
7
148 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 63

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 63

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 63

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 63

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

828 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