score:9
You shouldn't to try order the date as string;
var res = objUI.Where(x => x.LastLogin != null)
.GroupBy(x => x.LastLogin.Value.Date)
.OrderByDescending(x => x.Key)
.Select(x => new { LastLogin = string.Format("{0:MM/dd/yyyy}", x.Key) })
.ToList();
score:7
You're ordering by your string representation which starts with the month.
It looks like you're actually only interested in distinct dates though - grouping and then discarding everything other than the key is equivalent to just projecting and then taking distinct data. I'd also suggest avoiding an anonymous type unless you really need it - an anonymous type with only a single property is usually a bad idea.
Finally, if you're formatting a single value - certainly a DateTime
value - it's simpler to just call the ToString
method. In the code below I've explicitly specified the invariant culture too - otherwise you may find an unexpected calendar system is used...
So I'd write:
var res = objUI.Where(x => x.LastLogin != null)
.Select(x => x.LastLogin)
.Distinct()
.OrderByDescending(x => x)
.Select(x => x.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture))
.ToList();
Source: stackoverflow.com
Related Articles
- Groupby multiple date properties by month and year in LINQ
- I wants to date month and year add 2 month later in linq
- orderByDescending date, month and year
- Group by year and month with given date range using Lambda or LINQ Query
- How to group an SQL date column stored as int as Year Month using LINQ
- Search for month or year in a date using Linq based on condition
- How to get Date Month and Year Number using dataSet.Tables query
- LINQ how to Group data by date day and month and year
- LINQ: Group by month and year within a datetime field
- Using only the year part of a date for a WHERE condition
- how to check given date exist in month or not?
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- linq order by Month Name and Year (MMM yyyy string)
- How to add Month in date time and Compare with another date time in Linq?
- creating Linq to sqlite dbml from DbLinq source code
- LINQ - Group by Year and Month
- compare the date below current month c#
- Linq Select row Where date in Current month
- Dictionary of dates and values, finding value of max date per year in LINQ
- C# LINQ create month and year list
- Top 5 client performance per month in a date range using linq
- how can i wirte linq to compair the year and month f
- C# Linq to Entities get records where date range contains dates in the current month
- How to extract month from date time feild?
- Group items by month and by date
- Get rows filtered by month from a date column
- Get Month name and year from list
- ASP.net Getting Max Date of database Date column How to avoid Null in Date Column C# My code Attached
- Sorting Linq Result by Year and Month
- LINQ to Entities - filter on property of navigation property
- LINQ: How to convert from ISingleResult<T> to IQueryable<int>
- SQL command to LINQ (pivoting)
- Using a calculated value in the OrderBy clause with EF
- Retrieving an XElement from Dictionary object (<string, List<XElement>>) which contains List of XElements using Linq
- vb.NET Select distinct... how to use it?
- Update a value by jQuery
- Convert list of strings to list of simple objects
- Left Outer Join in Entity Framework (Linq). Member materializing to Null
- Reading columns from excel file using linq
- Linq group by a nullable timestamp (use only date part) and count
- Get unique object by key in MongoDB driver
- entity framework - underlying SQL statement
- Remove Items from sub list matching another sub list with Linq
- ExpressionTree ExpressionVisitor to change/replace query OrderBy Field
- Asp.net linq query
- Nested parent child lists where order by doesn't work
- C# Interface IEnumerable Any() without specifying generic types
- Remove <xyz> but gain data between
- ASP.NET Core - Get tuple with two values