How to search for a date and check if it exists in a datetime array in c#
How to search for a date and check if it exists in a datetime array in c#
I have a DateTime array, into which I want to insert certain dates. But beforehand I want to check, if the date already exists in the array. I am not able to use Contains or Exists in my code.
DateTime
Contains
Exists
for (int i = 0; i < TableFinal.Rows.Count; i++)
DateTime existingDates;
if (dsCSV.Tables[0].Rows.Count > 0)
var previousRatingDate = TableFinal.Rows[i]["RatingDate"];
if (existingDates.Contains(previousRatingDate))
//if not then insert previousRatingDate into array
Why aren't you able to use
Contains or Exists? Is this homework? Please show what you have tried– Gilad Green
Aug 27 at 7:02
Contains
Exists
i don't want to use a loop, i'm already inside an iteration, i tried applying if condition to my code and used array.Contains and array.Exists, but non of them worked for the datetime array.
– S.J.Lee
Aug 27 at 7:04
you can use
IndexOf property of an Array like Array.IndexOf(anyArray, value). if Index is greater than -1 then it results that it contains the said value i.e. Date in your case.– Rahul Hendawe
Aug 27 at 7:04
IndexOf
Array
Array.IndexOf(anyArray, value)
Then please show the condition with the
Contains and Exists because they are the proper way of doing so– Gilad Green
Aug 27 at 7:05
Contains
Exists
1 Answer
1
Make Extension Method like this:
Extension Method
using System;
using System.Linq;
public static class Extensions
public static void Add<T>(this T _self, T item)
if(Array.IndexOf(_self, item)== -1)
Array.Resize(ref _self, _self.Length + 1);
_self[_self.Length - 1] = item;
now you can use like :
DateTime existingDates= new DateTime ;
existingDates.Add<DateTime>(DateTime.Now);
Or You can use List Instead Arraylike this:
List
Array
var existingDates = new List<string>();
var previousRatingDate = TableFinal.Rows[i]["RatingDate"].ToString();
if (!existingDates.Any(item => item == previousRatingDate))
existingDates.Add(previousRatingDate);
I hope it help you.
thank you. i tried this but it gives an error at if (!datesList.Any(item => item == dateValue)) operator "==" cannot be applied to operands of type 'datetime' and 'object'
– S.J.Lee
Aug 27 at 7:26
@S.J.Lee you must parse your dateValue to
DateTime . like (DateTime)dateValue– Hossein
Aug 27 at 7:38
DateTime
(DateTime)dateValue
@S.J.Lee I update my answer.
– Hossein
Aug 27 at 7:46
i get this exception on this line var dateValue = (DateTime)TableFinal.Rows[i]["RatingDate"]; Exception : "Specified cast is not valid"
– S.J.Lee
Aug 27 at 9:07
@S.J.Lee What is
Type of RatingDate in DataTable?– Hossein
Aug 27 at 9:10
Type
RatingDate
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
so loop through the array and check whether each entry matches the date you're looking for. Break the loop if you find it. What have you tried so far?
– ADyson
Aug 27 at 7:00