Solved

Help with Linq To Dataset

Posted on 2014-09-17
7
144 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Basicfarmer
Comment Utility
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
Comment Utility
Not a problem, glad to help.
0
 

Author Closing Comment

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

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem Basicfarmer, glad to help.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

16 Experts available now in Live!

Get 1:1 Help Now