user8641201
user8641201

Reputation:

LINQ - Convert String to Datetime Order By

var chartData = (from r in _db.CashFlowHistory
where r.CompanyTaxCode == taxnumber
            group r by new
            {
                r.CreatedDate,
            }
            into g
            select new
{Date = g.Key.CreatedDate})

Result :

Date

<li>1.05.2019 
<li>8.05.2019 
<li>25.04.2019 
<li>23.04.2019
<li>6.05.2019 
<li>7.05.2019 
<li>28.04.2019 
<li>30.04.2019 
<li>26.04.2019 

Created date string format, I want to sort it by date.

Upvotes: 0

Views: 3311

Answers (3)

Michał Turczyn
Michał Turczyn

Reputation: 37347

Try this (assuming CreatedDate is string):

var dateArray = chartData
  .Select(e => DateTime.Parse(e.Replace("<li>", "").Trim()))
  .OrderBy(d => d)
  .ToArray();

or

var orderedArray = chartData
  .OrderBy(e => DateTime.Parse(e.Replace("<li>", "").Trim()))
  .ToArray();

Upvotes: 1

Code
Code

Reputation: 739

 if (date1!=null)
        {
            custQuery = custQuery.Where(c => DateTime.ParseExact(c.docDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) >= date1);
        }
        if (date2 != null)
        {
            custQuery = custQuery.Where(c => DateTime.ParseExact(c.docDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) <= date2);
        }

        return custQuery.ToList();

Upvotes: 0

Nivas Pandian
Nivas Pandian

Reputation: 424

chartData.OrderByDescending(x => x.CreatedDate)

Upvotes: 1

Related Questions