How to populate a List of strings from a DataTable? - vb.net

I'm trying to populate a List with a column of a DataTable in order to later convert it to a AutoCompleteStringCollection later. Is there a neater way to do it than the "good old" For Each?
Is there a way to use myDataTable.Rows.CopyTo for this task?
Cheers! = )

See this :- Best practice when converting DataColumn values to an array of strings?

No, I don't believe so. You can do some fancy LINQ expressions, but they all get boiled down to looping through the rows...

Related

How to make criteria with array field in Hibernate

I'm using Hibernate and Postgres and defined a character(1)[] column type.
So I donĀ“t know how to make this criteria to find a value in the array.
Like this query
SELECT * FROM cpfbloqueado WHERE bloqueados #> ARRAY['V']::character[]
I am not familiar with Postgres and its types but you can define your own type using custom basic type mapping. That could simplify the query.
There are many threads here on SO regarding Postres array types and Hibernate, for instance, this one. Another array mapping example that could be useful is here. At last, here is an example of using Criteria with user type.
Code example could be
List result = session.createCriteria(Cpfbloqueado.class)
.setProjection(Projections.projectionList()
.add(Projections.property("characterColumn.attribute"), PostgresCharArrayType.class)
)
.setResultTransformer(Transformer.aliasToBean(Cpfbloqueado.class))
.add(...) // add where restrictions here
.list()
Also, if it is not important for the implementation, you can define max length in the entity model, annotating your field with #Column(length = 1).
Or if you need to store an array of characters with length of 1 it is possible to use a collection type.
I hope I got the point right, however, it would be nice if the problem domain was better described.
So you have array of single characters... Problem is that in PG that is not fixed length. I had this problem, but around 10 years ago. At that time I had that column mapped as string, and that way I was able to process internal data - simply slice by comma, and do what is needed.
If you hate that way, as I did... Look for columns with text[] type - that is more common, so it is quite easy to find out something. Please look at this sample project:
https://github.com/phstudy/jpa-array-converter-sample

Improve performance in linq query

We have a linq query as shown below to display items first it should display records matching with the text and next with if the text contains in the string.But it is causing lot of performance issue. Could any one pls help how to improve it.
Dim result1 = From entry As IAutoCompleteEntry In oldList
Where entry.ToString.ToUpper.Contains(Me.Text.ToUpper())
Order By entry.ToString.IndexOf(Me.Text.ToUpper()), entry.ToString.ToUpper.StartsWith(Me.Text.ToUpper())
Descending
Select entry
You are calling ToUpper() at several places. Is it possible to get your list/array to have a ToUpper() before you get into this linq query?
I think you can also have another column for entry.ToString.IndexOf(Me.Text.ToUpper()), entry.ToString.ToUpper.StartsWith(Me.Text.ToUpper()) before getting to this linq query.
It might improve on performance....
First, I'm not sure what the significance of the .StartWith has. Since you are already getting the IndexOf, you would already know the answer of StartsWith (it should be 0).
Next, you really shouldn't be using ToUpper (as #RobertMcKee mentioned), instead you should be using case insensitive comparisons. In that case, you shouldn't need the .ToUpper's anymore either...
Finally, I was actually going to say use contains for your second statement, but you probably don't even need it. You could just sort descending based on your entry variable. Here is an updated query I wrote up:
dim result1 = From entry As IAutoCompleteEntry In oldList
Where (entry.IndexOf(Me.Text, StringComparison.OrdinalIgnoreCase) <> -1)
Order By entry Descending
Select entry

Querying datasets vb.net

I'm working with an external access database (.accdb) in VB.NET and I'm trying to limit the amount of times that my [Winforms] program pings the data since it slows it down pretty considerably. I figured I could do this by querying my in-memory dataset instead of continuously going back to the database itself. Though, unfortunately, I cannot seem to figure out how to query my in-memory dataset. I tried looking up LINQ but couldn't find any instructions on how to set it up.
Am I missing something? Is there a better way to do this?
Thanks so much! My starting code below...
locsql = "SELECT * FROM TABLE1)
Dim objAdapter As New OleDb.OleDbDataAdapter(locsql, objconn)
objAdapter.Fill(locdata, "BASE")
So I can easily do some basic things I need with locdata("BASE").rows.item("Item") but I have to do some stuff like
SELECT thing FROM TABLE1 WHERE this = this and that = that and I would just rather not keep going back to the database if possible.
Is it possible?
You need to use the Select method on each datatable you want to query. The syntax is very similar to the part after the WHERE clause in you normal SQL query. IE
locdata.tables(0).Select("ID = 47")
This returns an array of datarows.
If you want to use Linq against a DataTable you need to apply the AsEnumerable method then apply the usual Linq methods.
For example.
If you need to find all the customers having the Field Country equals to 'Canada' and then Sum the total of their Amount column you could write
Dim listOfCanadians = locdata.Tables(0).AsEnumerable() _
.Where(Function(row) row.Field(Of String)("Country") = "Canada") _
.ToList()
Dim totalCanadaAmount = listOfCanadians.Sum(Function(row) row.Field(Of Decimal)("Amount"))
What I ended up doing was breaking down my data filters and creating new tables based on a series of loops and if/else statements. all of these responses were very helpful, just didn't seem to work for what I needed. Thanks everyone!

Query all collection names

Is there a way to query all the collection names? I figured there must be a relatively straight-forward way since Raven Studio is already doing it?
Thanks.
You can use GetTermsAsync.
Using example:
DatabaseCommands.GetTermsAsync("Raven/DocumentsByEntityName", "Tag", "", 100)

Dynamic Pivot Query without storing query as String

I am fully familiar with the following method in the link for performing a dynamic pivot query. Is there an alternative method to perform a dynamic pivot without storing the Query as a String and inserting a column string inside it?
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx
Short answer: no.
Long answer:
Well, that's still no. But I will try to explain why. As of today, when you run the query, the DB engine demands to be aware of the result set structure (number of columns, column names, data types, etc) that the query will return. Therefore, you have to define the structure of the result set when you ask data from DB. Think about it: have you ever ran a query where you would not know the result set structure beforehand?
That also applies even when you do select *, which is just a sugar syntax. At the end, the returning structure is "all columns in such table(s)".
By assembling a string, you dynamically generate the structure that you desire, before asking for the result set. That's why it works.
Finally, you should be aware that assembling the string dynamically can theoretically and potentially (although not probable) get you a result set with infinite columns. Of course, that's not possible and it will fail, but I'm sure you understood the implications.
Update
I found this, which reinforces the reasons why it does not work.
Here:
SSIS relies on knowing the metadata of the dataflow in advance and a
dynamic pivot (which is what you are after) is not compatible with
that.
I'll keep looking and adding here.