I have a table in my database. This table is called Order. In this table I have some fields like DOCNUMBR,VENDORID,DOCDATE and so on.
I fetched all records in DataTable dt in my C# application.and now i want to filter records on three category. 1. by document Number. Ex from DOCNUMBR=1000 to 5000 2. by Vendor ID. Exfrom VENDORID=ACETRAVE0001 to DOLECKIC0001 3. by Document Date. Ex from DOCDATE=31/04/2014 to 1/04/2014
I have following SQL Query.
select * from Order where VENDORID='ACETRAVE0001' or VENDORID like 'a%' or VENDORID='DOLECKIC0001' or VENDORID like 'D%'
select * from Order where DOCNUMBR>='1000' and DOCNUMBR<='5000'
select * from Order where DOCDATE>='2013-09-26' and DOCDATE<='2014-09-26'
I want to do this filtration by using LINQ on DataTable in my C# Application. I have put the following code but I'm not satisfy.
private DataTable filterByRang(string _from, string _to, string flag,DataTable dt)
{
try
{
if (flag == "isDocument")
{
var Value = dt.AsEnumerable().Where(z => z.Field<string>("DOCNUMBR").StartsWith(_from ) ||z.Field<string>("DOCNUMBR").Contains(_from )||
z.Field<string>("DOCNUMBR").StartsWith(_to) || z.Field<string>("DOCNUMBR").Contains(_to));
dt = Value.CopyToDataTable();
}
else if (flag == "isDocDate")
{
var filter = dt.AsEnumerable().Where(x => x.Field<string>("DOCDATE").Substring(0, 9) == _from || x.Field<string>("DOCDATE").Substring(0, 9) == _to);
dt = filter.CopyToDataTable();
}
else if (flag == "isVendor")
{
var Value = from row in dt.AsEnumerable()
where row.Field<string>("VENDORID").ToLower().Contains(_from.ToLower()) || row.Field<string>("VENDORID").ToLower().Contains(_to.ToLower())
orderby row.Field<string>("VENDORID")
select row;
dt = Value.CopyToDataTable();
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
return dt;
}
Thank you in advance!
You can use Dataview instead of Linq. But didn't get the point of your dissatisfaction. See the filtration link below, http://social.msdn.microsoft.com/Forums/en-US/cc3b0486-1031-4113-afb6-29dfa6e0319b/how-to-filter-a-datatable-dynamically-using-linq?forum=linqprojectgeneral
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.