Solved

Help with Linq To Dataset

Posted on 2014-09-17
7
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

705 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