Creating Query on the fly - EF Linq - vb.net

I'm trying to convert a functional hard code query to a dinamic query. With my knowledge this is a impossible task.
Can anybody help me? This is the hard code query. This works fine:
Dim ret0 = qry.AsQueryable.Select(Function(f) New With { f.id, f.bint1, f.scampo1})
Where qry is a "return content table" comes from another query.
I've try:
Dim ret99 = qry.AsQueryable().Select(Function(f) ("new (key.id, key.bint1, key.scampo1)"))
and other variations but not is working.
If I do something like that:
Dim ret1 = qry.AsQueryable().Select("scampo1")
this works fine. But if I try more than one column only the very first one is returned.
Hope somebody could help me on that.

Related

visual basic find database total

I'm working on creating a visual basic program that can find the sum total of a column from a database file and display it in a label control, and have been stuck for a while. I was hoping that someone could help me out a little.
I've tried a few different ways to accomplish it and they each keep throwing out the same error.
Dim SumQuery = From popualtion In PopulationDBDataSet.City
Aggregate order In PopulationDBDataSet.City
Into Sum(PopulationDBDataSet.City.PopulationColumn)
lblTotalPop.Text = SumQuery.ToString
and
Dim SumQuery = Aggregate Populaton In PopulationDBDataSet.City
Into Sumorders = Sum(PopulationDBDataSet.City.PopulationColumn)
lblAvgPop.Text = SumQuery.ToString
Both attempts produce the error "method sum not accessible in this context". Sorry for this post being a bit long but I'm out of ideas on this.
Your usage should include the variable used in Aggregate function:
Try this:
Dim SumQuery = From popualtion In PopulationDBDataSet.City
Aggregate order In PopulationDBDataSet.City
Into Sum(order.PopulationColumn)

Microsoft Access VBA: need to get sql representation programmatically from existing query

I need to be able to get the sql "property" from an existing query. So for example I could define strRS as:
strRS = Me.RecordSource '.RecordSource returns the name of a query
Then pass that value to as yet undefined function UnknownFunction(strRS) that takes strRS as a value:
strSQL = UnknownFunction(strRS)
Then the desired output, strSQL would be the original SQL string that defines the query pointed to by the form's "RecordSource" property.
The closest I've been able to get is that there may be a solution using QueryDef, but that is for queries made on the fly? What should the UnknownFunction be?
Once I have the strSQL string then I can extract from it to make a related query.
Ok, it is trivial IF one doesn't make it too complicated. I probably was getting the wrong answer at first because I was confusing "QueryDefs" with "QueryDef", which I should have known.
So without further ado, the full working code is:
Function FCN_QClip(strQRY As String) As String
qds = CurrentDb.QueryDefs(strQRY).SQL
FCN_QClip = qds
End Function
?FCN_QClip("QueryName") will yield the desired result in the immediate window for an existing query. Thanks to Nathan.
The solution may also be found at Social MSDN.

Binding VB.net DataRepeater to dataview items at runtime with aggregation using linq

Hoping someone can help me out with what is probably a dumb question.
I'm trying to use a datarepeater to display data generated via LINQ from a datatable
I've managed to do this fine with a filtered existing datasource using:
Me.Tbl_52TableAdapter.Fill(Me.CBRDataSet.tbl_52)
Dim query =
From dlist In CBRDataSet.tbl_52.AsEnumerable
Where (dlist.Field(Of String)("TL") = "CTS 06")
Select dlist
query.CopyToDataTable().AsDataView()
DataRepeater1.DataSource = query
The problem being that I need to aggregate a field in the dataset into a count.
If I replace the query with:
Dim query =
From CountAgent In CBRDataSet.tbl_52.AsEnumerable
Group CountAgent By PBX = CountAgent.Field(Of String)("TL") Into Count()
Select Count
It then states that:
'CopyToDataTable' is not a member of 'System.Collections.Generic.IEnumerable(Of Integer)'
I've tried to get around it by changing the declaration to:
Dim query As IEnumerable(Of DataRow) =
Which compiles, but I have no idea if it works, and I cant check as I can't find a way to bind a label to the produced count col of the dataview.
If anyone can tell me what I'm doing wrong i'd be most appreciative.
Coming back to my own question, in case it helps anyone else: MSDN had the answer -
You need to overload copyToDataTable as described in
"How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow"
on:
http://msdn.microsoft.com/en-us/library/bb669096.aspx

VB.NET - Why does this sql command work as a string but not as a parameterized query?

Dim command_sbsp As New OleDbCommand("SELECT * FROM SNOMED_ORG_INFO WHERE ConceptID=#ID AND Genus='SALMONELLA' AND SubSpecies=#Subsp", SNOMED_DB)
For Each match As Long In choices
command_sbsp.Parameters.Clear()
command_sbsp.Parameters.AddWithValue("#Subsp", word)
command_sbsp.Parameters.AddWithValue("#ID", match)
The above doesn't work. The query attempt says there's nothing that matches that. But when I try it as a simple string concatenation, it works fine.
command_sbsp = New OleDbCommand("SELECT * FROM SNOMED_ORG_INFO WHERE ConceptID=" + match.ToString + " AND Genus='SALMONELLA' AND SubSpecies='" + word + "'", SNOMED_DB)
Myself and a coworker have stared at this quite a lot and we can't figure out why it's not working. The problem is in the value for #ID (since it still works when I leave the other one parameterized). The funny thing is, Just a few lines of code above it I have a different query that sets a parameterized value for the same ID, using the the same choices list that the For Each loop is getting the match variable from.
Choices is a list of longs, and when I use choices(0) to parameterize ID in a query, it works. But now down here in this loop I have the new match long, and it doesn't want to make it work for me. Any thoughts?
Despite the fact that the OLEDB class can use named parameters, it is actually just using a '?' marker in the background, so the parameters are in INDEX order. Your sql has the ID variable occurring before the Subsp variable.
Switch it around, like so:
For Each match As Long In choices
command_sbsp.Parameters.Clear()
command_sbsp.Parameters.AddWithValue("#ID", match)
command_sbsp.Parameters.AddWithValue("#Subsp", word)

Autocomplete list does not show all possible completions with BindingSource.Item

I've got a BindingSource for a DataSet. I'm fairly new to this whole binding business and databases, and it took me hours to figure out how to use BindingSource to get to an item, because the Row method was not included in the autocomplete. Not to confuse anyone, here's some sample code:
Dim somePreperty As String
Dim dataSet As New MyDataSet
Dim table As New MyDataSetTableAdapters.MyTableAdapter
Dim source As New BindingSource
source.DataSource = dataSet
source.DataMember = "SomeMember"
table.Fill(dataSet.SomeMember)
lablCabinet.DataBindings.Add("Text", source(0), "MemberID") '<This works fine>'
someProperty = source.Item(0).Row("ProductModel") '<So does this>'
The code runs perfectly and does exactly what I want. My problem is the following: When I've typed in source.Item(0)., autocomplete does not display Row in the list. Is this perhaps not the best way to do what I'm doing? Is there a reason it's hidden? Is this a good coding practice to do so? The fact that is wasn't there took me lots of time Googling, so I'm trying to figure out whether it's a Visual Studio glitch or my own.
Thanks in advance! = )
source.item(0) returns an object, so intellisense has no idea what is is.
You know what it should be, the compiler does not.
If you cast it first to a table or assing it to a table, intellisense will kick in.
So either:
ctype(source.item(0),datatable)
Or
dim tbl as datatable=source.item(0).