I'm trying to check if the field data of my DataTable records is set to '0' or '1'. All my local record is saved into local_ds DataTable. Now, the record that I want check is this: 21a956af-f304-4c72-97cf-1ef08e8719fc
for a better vision I paste here the content of my table (that is also the content of my DataTable local_ds):
How you can see the record that I want check have the field data set to 0. Now I perform the research through this code:
Dim local_data = local_ds.Tables(0).Select(String.Format("GUID = '{0}'", "21a956af-f304-4c72-97cf-1ef08e8719fc"), String.Format("data", 1))
The code above use LINQ to take the result, anyway, I pass the GUID field to search and the field data as 1. This code should be return local_data.length equal to 0 but, instead, return 1 and this is wrong, 'cause I want to check only if the field data is 0 or 1. In this example the result should be local_data.length = 0 'cause in the LINQ query I specified clearly that I want find the record with GUID = x and data = 1.
I already know that this record exists in the database, the local_data variable must help me to recognize which type of valorization the data field have.
So, what I did wrong?
No, in the code above, you are not using LINQ.
DataTable.Select is a method available starting from the 1.1 version of NET Framework and exists in four possible overloads.
The one you are using is the one that takes, as first parameter, the WHERE condition and, as second parameter, the SORT order. So you are not really passing a condition WHERE .... AND Data = 1 but the Data=1 it is interpreted as a sort order of some kind.
The correct string for the WHERE parameter should be
Dim where as String = String.Format("GUID = '{0}' AND Data = {1}", _
"21a956af-f304-4c72-97cf-1ef08e8719fc", 1)
Dim local_data = local_ds.Tables(0).Select(where)
Related
I have a question about using LINQ to access a Database and trying to make use of it's version of accessing the LIKE comparison operator.
I know that LINQ has .Contains(), .StartsWith(), and .EndsWith() as comparison methods. However I am wondering if there is a way to programatically imcorporate SQL Wildcards into a LINQ statement without explicitly using these query operators. Let me explain my situation.
I am writing a program that accesses a database, and part of the program is a search window which the user can use to help them find specific database data. I would like to try and incorporate SQL Wildcards into the textbox fields for these search pages.
For example if a user enters the input 17% I'd want the program to check for anything in that specific column that starts with a 17. The same is true with %17 and 17 where I'd want it to search for columns that end with, and contain the values.
Currently, this is the code I have for my search method:
Public Function Data_Search(sData As List(Of String), DB As CustomDataContext) As DataGridView
Dim gridData As New DataGridView
Dim query = From p In DB.Parts
Select p.Part_Number, p.Part_Description, p.Supplier.Supplier_Name
for i = 0 To sData.Count - 1
If Not sData(i).ToString() = "" Then
Select Case i
Case 0
Dim partNum As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Number.Contains(partNum))
Case 1
Dim description As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Description.Contains(description))
Case 2
Dim supp As String = sData(i).ToString()
query = query.Where(Function(x) x.Supplier_Name.Contains(supp))
End Select
End If
Next
gridData.DataSource = query.ToList()
Return gridData
End Function
So right now I am trying to see if there is a way for me to modify the code in a way that doesn't essentially involve me putting a substring search into each Case section to determine if I should be using StartsWith(), Contains(), or EndsWith.
Thanks for your time.
If you are using LINQ to SQL, and you are talking to Microsoft SQL Server, then you can use SQLMethods.Like to implement SQL LIKE:
query = query.Where(Function(x) SQLMethods.Like(x.Part_Number, partNum))
I need to migrate a CR report to RS.
I have two Methods, that are basically two queries, one is called "Products" the other one is called "Volume".
Both these datasets have a field called ID.
The idea is to count ProductsID = VolumeID. Can I use a lookup as an expression inside RS that counts when lookup is not equal to Nothing? Or I have to do a code to do that?
EDIT:
Let me add more information.
Crystal Report is managing these in the Data Layer of the web app like this:
DataRow drVol = vols.Rows.Find(new Object[] { id, idEzd });
if (drVol != null)
{
if (drVol["Volume"] != System.DBNull.Value)
cRow.Volume = (isNormalReport ? Convert.ToDecimal(drVol["Volume"]) : Convert.ToDecimal(drVol["Volume"]));
else
cRow.Volume = 0;
dset.Compradores.Rows.Add(cRow);
}
So in this case I verified that RS is giving me an X amount of rows while CR is giving me another amount of rows.
The Idea is to duplicate this on an expression or VB code inside RS.
As you can see, we have to filter the drVol != null, RS is bringing all the rows.
I know a Lookup function can be used in this case as:
Lookup(Fields!id.Value & Fields!idEzd.Value, Fields!id.Value & Fields!idEzd.Value,Fields!Givemethisfield.Value,"Dataset")
But can I use this Lookup expression inside a code to count?
The counter should be something like this (Then I should add the lookup or an if that equals that vlookup, can the code see the two datasets?):
Dim count=0 as integer
Public Function Counter() as Integer
count = count + 1
return count
End Function
Public Function GetCounter () as integer
return count
End Function
Then create a Calculated Field for example "Counter" with the expression
=Code.Counter()
And then make the sum of that calculated field:
=Sum(Fields!Counter.Value)
But is not working, not even the counter, is giving me 0.
Sorry if I made a mistake in the Visual Basic code, I don't program in Visual.
EDIT2:
I saw some workaround on MSDN, something like:
Lookup(Fields!id.Value & Fields!idEzd.Value, Fields!id.Value & Fields!idEzd.Value,Fields!Givemethisfield.Value,"Dataset").Lenght
It makes sense as a lookup is an Array.
The problem is, either if I choose one scope or the other, I still have the problem that some fields can't be seen for example if the fields id and idezd are on Dataset1 and Givemethisfield is on Dataset2, it will either not see the id,idEzd or Givemethisfield.
I am using VB.NET and I need to use Datatable.Select to get a row which has a matching value.
Consider I have Datatable_A, which has many rows. I am interested to get row/rows which matches my search criteria. In this case, I must get a row (RowA), which has AnswerA stored in Column_A.
I know I can easily use the method below to find the answer:
RowA = Datatable_A.Select("Column_A = 'AnswerA'")
However, value 'AnswerA' is stored in Variable_A.
I have tried using
RowA = Datatable_A.Select("Column_A = 'Variable_A'")
Unfortunately, I still could not get the datarow, whose Column_A = 'Answer A'.
I have studied the explanation in this website DataView RowFilter Syntax [C#], but I could not find any clue there.
Thank you.
If you want to select a subset of your datatable rows using the Select method and a variable that contains the value to filter your rows you need to write
Dim Variable_A = "MySearchCriteria"
Dim rowsSelected = Datatable_A.Select("Column_A = '" & Variable_A & "'")
This means that you want all the rows in which the Column_A (a string column) contains exactly the value "MySearchCriteria" stored in the Variable_A. The filter condition is created concatenating the literal string describing the name of the column and the operator with the content of the Variable_A. Since Column_A is assumed to be a string column then you need to encapsulate the Variable_A between single quotes.
And remember that Select returns an array of DataRow that match the filter expression.
For Each row in rowsSelected
Console.WriteLine(row("Column_A").ToString)
Next
I have 2 tables. An asset table that links using a foreign key to a device table.
To get the device_name field of the asset, in linq i would use:
dim device as string = result.device.device_name
The Result object contains the unique record from an earlier linq to entity framework command.
Now I have a need to allow end users to specify their fields they want in a report. I will end up having a string name of the field, so tried using reflection
dim name as string = result.GetType().GetProperty("asset_name").GetValue(result)
This returns the asset_name field so i thought great, i should be able to get the relating foreign table field value.
dim device = result.GetType().GetProperty("device").GetValue(result).GetType.GetProperty("device_name").GetValue(result.device)
This works however i had to specify the object. As i will have other tables linking to the first table, would i have to write additional code to check what object and manually specify? Or am i going into this wrongly? Any help and advice appreciated.
I couldn't find a cleaner solution, so this is how i did it:
dim field as string ="device.device_name"
dim output as string =""
dim subtable as string =""
If field.Contains(".") Then
subtable = field.Substring(0, field.IndexOf("."))
field = field.Substring(field.IndexOf(".") + 1)
End If
If subtable <> "" Then
Select case subtable
Case "device"
If Not IsNothing(result.device.GetType().GetProperty(field)) Then
output = result.device.GetType().GetProperty(field).GetValue(result.device)
End If
End Select
Else
If Not IsNothing(result.GetType().GetProperty(field)) Then
output= result.GetType().GetProperty(field).GetValue(reult)
End If
End If
Return output
I have a method that takes in an id, a start date, and and end date as parameters. It will return rows of data each corresponding to the day of the week that fall between the date range. The rows are all doubles. After returning it into a DataTable, I need to be able to use LINQ in VB.NET to return the maximum value. How can I achieve this? Here is the initial setup?
Dim dt as DataTable = GetMaximumValue(1,"10/23/2011","11/23"/2011")
'Do linq query here to return the maximum value
The other alternative is to just return a Maximum value just given an id which would be slightly easier to implement, so the method would look like this:
Dim dt as DataTable = GetMaximumValue(1)
'Do linq query here to return maximum value
Important
What if I want to query against a DataRow instead of a DataTable and the column names are not the same, they are something like MaxForMon, MaxForTue, MaxForWed`, etc... and I need to take the Maximum value (whether it is MaxForMon, MaxForTue, or some other column). Would an option for this be to alias the column returned in the stored proc? The other thing I thought about was instead of doing this in LINQ, I probably should just handle it in the T-SQL.
You can use the DataSet Extensions for Linq to query a datatable.
The following query should help.
Dim query = _
From value In dt.AsEnumerable() _
Select value(Of Double)("ColumnName").Max();
If you don't now in which column will hold the maximum you could use: (C#)
var result = from r in table.AsEnumerable()
select r.ItemArray.Max();