ERR
ERR

Reputation: 415

Linq with DateTime.Parse

I have a list "Sites" with one of the items as "From" and "To". It is defined as string and is in the format "MM/yyyy". When I try to sort the list based on the year, I'm facing a small problem.

Data for "From" and "To" is

01/2012
04/2012
01/2013
06/2012

When I sort the list by using orderby, the output I'm getting is

01/2012
01/2013
04/2012
06/2012

which is incorrect.

List<Site> List = new List<Site>();
                DataSet DS = ClientDB.Sites(Id);
                if (DS.HasTable0AndRows())
                {
                    IEnumerable<DataRow> _DataRow = DS.Tables[0].AsEnumerable();
                   List = _DataRow.Select(x => new Site()
                    {
                        FileNum = x["File_Num"].ToString(),
                        From = x["From"].ToString(),
                        To = x["To"].ToString(),
                    }).ToList();
                }
                return List.OrderBy(x => x.FileNum).ToList();
            }

I understand that I have to use DateTime.Parse inorder to convert the From and To but how do I use the DateTime.Parse in the above case when i return the list?

Upvotes: 0

Views: 2114

Answers (3)

Farzin Kanzi
Farzin Kanzi

Reputation: 3435

If you problem is not solved yet use this:

Add this function beside your method:

    public int Compare(string a)
    {
        string[] arr = a.Split('/');
        return int.Parse(arr[1] + arr[0]);
    }

And use like this sample:

     List<string> dates = new List<string> { "01/2012", "04/2012", "01/2013", "06/2012" };
     dates = dates.OrderBy<string,int>(Compare).ToList();

Upvotes: 0

Thomas D.
Thomas D.

Reputation: 1061

This should do the trick:

List<Site> List = new List<Site>();
DataSet DS = ClientDB.Sites(Id);
if (DS.HasTable0AndRows())
{
  IEnumerable<DataRow> _DataRow = DS.Tables[0].AsEnumerable();
  List = _DataRow.Select(x => new Site()
  {
    FileNum = x["File_Num"].ToString(),
    From = DateTime.ParseExact(x["From"].ToString(), "mm/yyyy", CultureInfo.InvariantCulture),
    To = DateTime.ParseExact(x["To"].ToString(), "mm/yyyy", CultureInfo.InvariantCulture),
  }).ToList();
}
return List.OrderBy(x => x.From).ToList();

I am assuming that you want the From and To of type DateTime instead of string.

If you want to keep the types to be strings then you should use Uladzimir Palekh answer.

Upvotes: 0

Uladzimir Palekh
Uladzimir Palekh

Reputation: 1871

You need custom comparer.

class DateComparer : IComparer<string>
{
    public int Compare(string a, string b)
    {
        var a_date = DateTime.ParseExact(a, "MM/yyyy", null);
        var b_date = DateTime.ParseExact(b, "MM/yyyy", null);
        return a_date.CompareTo(b_date);
    }
}

Usage:

return List.OrderBy(x => x.From, new DateComparer()).ToList();

Upvotes: 5

Related Questions