I have a problem in a C# project. I am using the Select method with a DataTable object.
If I put a '-' in the search string I select nothing.
So here is a code sample of what I have:
DataTable table;
DataRow[] rows = table.Select("[Radio Name] LIKE '*Lounge-MP3-96*'");
But there is a column with:
Radio Name = 1.FM - The Chillout Lounge-MP3-96
Have I to escape characters? How?
I've just tried
DataTable table;
DataRow[] rows = table.Select("[Radio Name] LIKE '*Lounge*'");
It works!
So it seems really related to the "-"....
I don't think it's the "-". I thought wildcards needed to be percent symbols for the datatable select (it mimics SQL): "%"?
Try this:
DataTable table = GetTableFromSomewhere();
DataRow[] rows = table.Select("[Radio Name] LIKE '%Lounge-MP3-96%'");
Also, your example doesn't populate the table with anything in the first place so it wouldn't work - I'm assuming you do populate your table somehow.
Related
I have a datatable with name field and can have names(firstname surname)with white spaces between two words.
when I use a select method to query the records, I get nothing in return
example:
DataRow[] results = datatable.Select("Name = 'FITUR DISPELOR'");
Console.WriteLine(results.Count().ToString());
gives me 0 count of returned results.
But if I write the following, I have a matching record.
DataRow[] results = datatable.Select("Name = ''");
Console.WriteLine(results.Count().ToString());
Can someone help me out how can I query the full name in a data table? I am using c# for this.
I want to filter data table based on some condition .
I have a data table like that
Tabel A
MobileNo Email
9999999999 test#test1.com
8888888888 test#test2.com
9999999999 test#test5.com
7777777777 test#test6.com
I want to get distinct value based on mobile also I need data that not exits in distinct table but table A have
Like
Distinct Table Ignore Table
MobileNo Email MobileNo Email
9999999999 test#test1.com 9999999999 test#test5.com
8888888888 test#test2.com
7777777777 test#test6.com
I have also tried googling but that are not very much understandable.
Thanks in Advance
You can use the select method on DataTable
Dim dtFiltered As New DataTable()
dtFiltered = dtAllData.DefaultView.ToTable(True, "MobileNo")
dtAllData is the DataTable containing all records
dtFiltered contains only Distinct Records
For more details check MSDN article
UPDATE based on your comment
In that case you need to use except as in
datatable-comparison-using-linq-except-intersect-union
From all records extract distinct records to second DataTable and then use Except to compare the first and second DataTable
Thanks for all your help
Finally I got the solution
Dim ValidData = (From row In tbValid.AsEnumerable()
Let Mobile = row.Field(Of String)("Mobile")
Group row By Mobile Into DupMobile = Group
Select DupMobile.First).ToArray
Dim Ignoredata = tbValid.AsEnumerable().Except(ValidData.AsEnumerable(), DataRowComparer.Default).ToArray
From your example all you have done is order your mobile no. in desending order which means you could then create a SQL to say something like: ORDER BY MobileNo DESC at the end of the statement. At least it will display the same way
Or are you meaning:
Dim dv As DataView = New DataView(DataTable.DataView)
dv.RowFilter = "%" &...
I have a DataTable (Ado.Net) with column 'Status'. This column holds the values (in each records)
['Red','Green','Blue','Yellow','White','OtherColors']
I want to select all the rows which status value not Red,Green,Blue
What the kind of filter expression to use to select data with my proposed criteria. So i want to achive some thing like we used in sql query ( WHERE Status NOT IN ('Red','Green','Blue')
NB:This project is running .NET 2.0 i cant use linq
I have tested it, it works as desired:
DataRow[] filtered = tblStatus.Select("Status NOT IN ('Red','Green','Blue')");
The resulting DataRow[] contains only DataRows with OtherColors, Yellow and White.
If you could use LINQ i'd prefer that:
string[] excludeStatus = {"Red","Green","Blue"};
var filteredRows = tblStatus.AsEnumerable()
.Where(row => !excludeStatus.Contains(row.Field<string>("Status")));
Without Linq you can use the rowfilter of a DataView like this
public DataTable GetFilteredData(DataTable table, string[] filterValues)
{
var dv = new DataView(table);
var filter = string.join("','", filterValues);
dv.RowFilter = "Status NOT IN ('" + filter + "')";
return dv.ToTable();
}
Supposing your datatable is part of a typed dataset you can use Linq to datasets, from which you could something like:
var records =
from record in datatable
where !record.Status.Contains('Red','Green','Blue')
select record;
Even if you don't have a typed dataset Linq to datasets is your answer. You would need to some casting, but not to difficult.
I have a datatable that has been bound to a Gridview in code behind. This means i would have to sort columns in code behind and when searching for a record i would imagine that would be done in code behind too.
The code i have to sort the gridview is
Private Function GetCustData As Datatable
Dim dt as new datatable
dt = GetDataFromBusinessLayer(CustomerID)
Return dt
End Function
And the code to sort
Private Sub gv_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
If e.SortExpression = "Name"
gv.DataSource = GetCustData '.DefaultView.Sort = "Name" & "DESC"
gv.DataBind()
End If
End Sub
As you can tell i tried using .DefaultView.Sort = "Name" & "DESC" but this didnt work and got the error Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. Searching around most are using Viewstate but that doesnt seem applicable in my case.
Can anyone advise how i would sort one/multiple columns?
In addition i will like to have a textbox which will search for the Name column. I think the above should suffice when it comes to it but if not could someone put me on the right track so i can work towards that now rather than having to change what im doing at a later stage?
First of all, your current code does not have a space. To sort by Name DESC your Sort string would need to be: "Name DESC".
To set the Sort string to multiple columns, just add a comma between each one, like this:
Dim dt as new datatable = GetCustData(1)
dt.DefaultView.Sort = "Name DESC, FirstName ASC"
gv.DataSource = dt.DefaultView
gv.DataBind()
I think you can create a list of rows
List<DataRow> list = dt.AsEnumerable().ToList();
and then when you have a list you can now use sort
list.OrederBy(c=>c.///specific value in your list)
What about using Linq to do the sort for you.
Dim dt as new datatable
dt = From dr as Datarow in GetDataFromBusinessLayer(CustomerID)
Order by dr.item("First Sort field), dr.Item("Second Sort Field")
Select dr
Return dt
... I did this without the IDE, but that should do the trick, I believe.
Here's one exmaple online - Sorting DataTable with Linq
Hope this helps.
I have a DataTable imported from Excel file.
Data i need is only unique from specific columns of the DataTable.
The unique data i meant is like when a command DISTINCT is used in SQL Select Query.
I want to get the list of the unique data from the DataTable Column and put them into List
I think LinQ can be used for this matter but i'm not so familiar with it.
I was thinking of code like this below
var data is from MyDataTable
where MyDataTable.ColumnName = "SpecificColumn"
select MyDataTable["SpecificColumn"]).UniqueData;
List<string> MyUniqueData = new List<string>();
foreach(object obj in data)
{
if(MyUniqueData.NotContain(obj))
MyUniqueData.add(obj);
}
I hope someone can drop off some knowledge to me.
var unique = data.Distinct().ToList();
What you're looking for is .Distinct(). See MSDN documentation here. You can specify your own comparer if you need something specific and it will return only unique records.
If you have a Datatable or DataView, inorder to get unique records from a column, you have to write this.
this would be simple.
DataTable dtNew = dt.DefaultView.ToTable(true, "ColName"); // for Datatable
DataTable dtnew= dv.ToTable(true, "ColName"); // for DataView