Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help with Linq To Dataset

Posted on 2014-09-17
7
Medium Priority
?
182 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 64

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 64

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

577 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