I have a table with the value of a range of dates. If I input a date then it should look that date within a date range in the table and give me a value. I am working in C#.
DateFrm |DateTo |ivalue
----------|----------|-----
2019-01-01|2019-02-01|10
2019-02-02|2019-03-01|11
2019-03-02|2019-04-01|12
If I enter a date eg 2019-02-09 then it should return me value 11. Please help me to find my values in C#. Thank you.
I was trying something like.. but never get it sorted.
if(obj.mytable.rows.count>0)
{foreach }
...
Suppose that dt
is your DataTable:
string example = "2019-02-09";
DataRow[] results = dt.Select($"DateFrm <= #{example}# AND DateTo >=#{example}#");
Console.WriteLine(results[0]["ivalue"]);
Output:
11
Run a loop for the rows to check the condition as specified below
for (int i1 = 0; i1 < table.Rows.Count; i1++)
{
DataRow row = table.Rows[i1];
var datefrm = DateTime.Parse(row["DateFrm"].ToString());
var dateto = DateTime.Parse(row["DateTo"].ToString());
if (inputDate >= datefrm && inputDate <= dateto)
{
return row["ivalue"];
}
}
It's quite simple using LINQ.
void Main()
{
var lst= new List<DateRange>();
var dr = new DateRange(new DateTime(2019, 1, 1),new DateTime(2019, 2, 1),10);
lst.Add(dr);
dr = new DateRange(new DateTime(2019, 2, 2), new DateTime(2019, 3, 1), 11);
lst.Add(dr);
dr = new DateRange(new DateTime(2019, 3, 2), new DateTime(2019, 4, 1), 12);
lst.Add(dr);
var searchFor = new DateTime(2019, 2, 9);
var res = (from x in lst
where x.DateFrm <= searchFor
&& searchFor <= x.DateTo
select x.IValue).Single();
Console.WriteLine($"result:{res}");
}
public class DateRange
{
public DateTime DateFrm { get; set; }
public DateTime DateTo { get; set; }
public int IValue { get; set; }
public DateRange(DateTime dateFrm, DateTime dateTo, int iValue)
{
DateFrm = dateFrm;
DateTo = dateTo;
IValue = iValue;
}
}
You could query your DataTable
with LINQ, using the extension method AsEnumerable
. To use this method, a reference to the System.Data.DataSetExtensions
assembly is required.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var dt = new DataTable();
dt.Columns.Add("DateFrm", typeof(DateTime));
dt.Columns.Add("DateTo", typeof(DateTime));
dt.Columns.Add("ivalue", typeof(int));
dt.Rows.Add("2019-01-01", "2019-02-01", 10);
dt.Rows.Add("2019-02-01", "2019-03-01", 11);
dt.Rows.Add("2019-03-01", "2019-04-01", 12);
var searchFor = new DateTime(2019, 2, 9);
DataRow[] results = dt.AsEnumerable().Where(row =>
searchFor >= row.Field<DateTime>("DateFrm") &&
searchFor < row.Field<DateTime>("DateTo")).ToArray();
foreach (var result in results)
Console.WriteLine(String.Join(", ", result.ItemArray));
Output:
02/02/2019 00:00:00, 03/01/2019 00:00:00, 11
I am sorry that I couldn't make my question easier, potentially I haven't mentioned about I am using LINQ.
I have created a procedure after getting help from here.
where DateFrm<=mysearchdate and DateTo<=mysearchdate
Later in the flow, I have called that in the code
var vl = (from s in obj.st_getiValueSearch(mysearchdate.Value) select s.ivalue).Single();
Thanks again for prompt help.
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.