I'm trying to get data from both Tables, where Usage.ArticleId equals Article.idArticle. Below are my Tables.
I load the Dataset and Datatable like that:
DataSet1 = CType(Me.FindResource("DataSet1"),
KleinteilContainerVerwaltung.DataSet1)
DataSet1.ReadXml(XMLPath)
Dim article As DataTable = DataSet1.Article
Dim usage As DataTable = DataSet1.Usage
Below is my attempt using LINQ. But the Select doesn't work.
Dim queryResults = From art In article
Group Join use In usage On
art.Field(Of Int32)("idArticle") Equals use.Field(Of Int32)("ArticleId")
Into _list = Group
Select art.Field(Of Int32)("Article")
And is there a way to use art.idArticle instead of art.Field(Of Int32)("idArticle")? And are there other ways instead of using LINQ?
Related
I've been tasked to take over some legacy reporting written in Visual Basic and have been tasked with updating some of the reports which the only way I can see is to use LINQ which I'm having a heck of a time with and I've spent the past week on Google trying to sort it out but I'm just missing "something".
What I have is a datatable that has like 10 columns and I need to group on two taking the results of the grouping and placing it into another datatable.
What I have so far is...
Dim Rep_Country_DataTable As New System.Data.DataTable
Dim Rep_Country_Group As IEnumerable = From Data_Row In Export_DataTable.Select("REP IS NOT NULL").AsEnumerable
Group Data_Row By Rep = Data_Row.Field(Of String)("REP"),
Country = Data_Row.Field(Of String)("COUNTRY") Into Group
Select Rep, Country
The LINQ works and I can see the grouped data in "Rep_Country_Group". My problem now is getting that data into the "Rep_Country_Group_DataTable" datatable, that's the part I can't figure out.
I would appreciate any help with this as I know it's something simple I'm missing.
Thanks to those that replied. Using the replies I was able to refine my Google searches and found my solution.
Here's the revised code that solved my issue...
Dim Rep_Country_DataTable As New System.Data.DataTable
With DomainCountry_DataTable.Columns
.Add("REP", GetType(String))
.Add("COUNTRY", GetType(String))
End With
Dim Rep_Country_Group As IEnumerable(Of System.Data.DataRow) = (From Data_Row As System.Data.DataRow In Export_DataTable.Select("REP IS NOT NULL").AsEnumerable
Group Data_Row By Rep = Data_Row.Field(Of String)("REP"),
Country = Data_Row.Field(Of String)("COUNTRY") Into Group
Select New With {.Rep = Rep,
.Country = Country}).Select(Function(new_row)
Dim New_DataRow As System.Data.DataRow = DomainCountry_DataTable.NewRow()
New_DataRow("REP") = new_row.Rep
New_DataRow("COUNTRY") = new_row.Country
Return New_DataRow
End Function)
I have a pretty long code which creates two datatables and fills them with data at runtime. And i need to create one table, but not the way merge does it but like adding columns from one datatable as new columns(but filled with data) to the other one. Best example i can thing of is Join in SQL but i will try to draw some example. I can not put my code here as i said it't too long.
Example:
(only column names but imagine that under each |something| is a lot of rows)
Table1:
Date|AA|AA2|AA3|AA4|AA5|AA6|
Table2:
Date|BB|BB2|BB3|BB4|BB5|BB6|
Result:
Date|AA|AA2|AA3|AA4|AA5|AA6|BB|BB2|BB3|BB4|BB5|BB6|
The DataTable.Merge method is perfectly capable of yielding your desired result. As your two tables have only one column in common Date, I am assuming that it is PrimaryKey of at least Table 1.
Dim dt1 As New DataTable
Dim pk As DataColumn = dt1.Columns.Add("Date", GetType(DateTime))
dt1.PrimaryKey = {pk}
dt1.Columns.Add("C1")
dt1.Columns.Add("C2")
dt1.Rows.Add(#1/1/2018#, "c1", "c2")
Dim dt2 As New DataTable
Dim pk2 As DataColumn = dt2.Columns.Add("Date", GetType(DateTime))
dt2.Columns.Add("C3")
dt2.Columns.Add("C4")
dt2.Rows.Add(#1/1/2018#, "c3", "c4")
dt2.Rows.Add(#1/2/2018#, "c3a", "c4a")
Dim dt3 As DataTable = dt1.Copy
dt3.Merge(dt2)
DataGridView1.DataSource = dt3
This code yield this result:
I am working on VS 2010 - Vb.Net, Having a syntax clarification on Filter & Merge DataSet
My dataset - ds has 23 tables. In which, i need to filter one Table with certain criteria.
tempDs.Merge(ds.Tables("p_tree_categories").Select( "GROUP_CODE <> 'PA.43.948'"))
On writing Merge Syntax,
I am able to see only selected table : p_tree_categories in the tempDs. We need the remaining 22 tables along with the filtered record of p_tree_categories.
How can i achieve this ?
It sounds like you only want the filtered rows of table p_tree_categories. This being the case, I would:
Generate a copy of the p_tree_categories which only contains the rows you are interested in.
Remove the existing p_tree_categories from tempDs.
Merge the copy into tempDs as p_tree_categories.
These steps could be implemented something like this:
Dim originalTable As DataTable = tempDs.Tables("p_tree_categories")
Dim filterView As DataView = New DataView(originalTable)
filterView.RowFilter = "GROUP_CODE <> 'PA.43.948'"
Dim filteredTable As DataTable = filterView.ToTable
filteredTable.TableName = "p_tree_categories"
' Remove old, add the new.
tempDs.Tables.Remove(originalTable)
tempDs.Tables.Add(filteredTable)
I have a typed datatable with the columns month and money. My goal is to get an object that I can bind to a dgv (table or list) where the money is grouped by month.
In SQL I would accomplish this with
Select month, sum(money) from table
group by month
I tried (according to this question )
Public Function Get Overview(...)
dim query =
from dt In _dataset.Table.AsEnumerable()
Group By dt.Week
Into Test = Sum(dt.money)
Select Test
Return Test
End Function
I cannot run this but the returntype is IEnumerable(of Double) so to me it looks I do not get the Table or List back that I can bind to my dgv. Could anyone tell me what is wrong with my query?
Edit:
This is Linq to datasets.
Declaration and instance
Privat _dataset as Dataset1
Private _adapter as New Dataset1TableAdapters.Table
_dataset = New Dataset1
_adapter.Fill(_dataset.Table)
With Select Test you explicitly select only the aggregated values of the money column.
Your query should look like:
Dim query = From dt In _dataset.Table.AsEnumerable()
Group By week = dt.Week
Into result = Sum(dt.money)
Note that this yields an IEnumerable(Of ...) of an anynomous type. If you want to use the result outside the current method, create a type that represents this result or use e.g. the Tuple class, like:
Dim query = From dt In _dataset.Table.AsEnumerable()
Group By week = dt.Week
Into result = Sum(dt.money)
Select Tuple.Create(week, result)
This yields an IEnumerable(Of Tuple(Of TheTypeOfWeek, TheTypeOfMoney)) which you can bind to your DGV.
I have a query that can be summarised in SQL as follows;
Select
S.StockCode
From
StockToCheck As S
Inner Join
GetPOSStock(S.StockCode) As C
On S.StockCode = C.StockCode;
I'm trying to do the same in Linq but it seems I cannot pass a parameter to the function I am joining on as it has not been parsed by Linq.
I imagine it would look like this (vb);
Dim x = From S In StockToCheck
Join C In GetPOSStock(S) On S Equals C.ProductCode
Where the S var is a list of strings. This gives the error 'S' is not declared and points to the S in the function call / join (GetPOSStock). So it does not seem possible to do this in Linq, can anyone confirm?
Thanks in advance
Ryan
The following worked for me:
Using dc As New TestDC
Dim x = From s In dc.Stocks
From c In dc.GetPOSStock(s.Code)
Where s.Code = c.Code
Select s.Code
End Using
I assumed that GetPOSStock is a table-valued function in your DB, so I created one accordingly. I also created separate classes for Stock and for POSStock and assigned POSStock as the return value for GetPOSStock using Linq-to-SQL designer.