score:8
(from pro in Products.ToList()
let max = Max(pro.DateSend, pro.DateEdit)
select max).Max()
static DateTime? Max(DateTime? a, DateTime? b)
{
if (!a.HasValue && !b.HasValue) return a; // doesn't matter
if (!a.HasValue) return b;
if (!b.HasValue) return a;
return a.Value > b.Value ? a : b;
}
score:1
((from pro in Products.ToList()
select pro.DateSend).Union(
from pro2 in Products.ToList()
select pro.DateEdit
)).Max();
score:7
If you're not opposed to ditching the query syntax it's simpler to do:
DateTime max = Products.Max(p=>p.DateSend > p.DateEdit ? p.DateSend : p.DateEdit);
score:0
Same code but with One LinQ
(From pro in Products.ToList
Let max = {pro.DateSend, pro.DateEdit}.Max
select max).Max
If pro items not a valid DateTime, It will crash or return unexpected result.
Other solution(Parsing the Products list items):
(From pro in Products.ToList
Let DSend as DateTime = Convert.ToDateTime(Iif(ValidDateTime(pro.DateSend), pro.DateSend, DateTime.MinValue))
Let DEdit as DateTime = Convert.ToDateTime(Iif(ValidDateTime(pro.DateEdit), pro.DateEdit, DateTime.MinValue))
Let MaxDate as DateTime= {DSend, DEdit}.Max
select MaxDate).Max
private function ValidDateTime(ByVal Dat as Object) as Boolean
if Dat Is Nothing OrElse IsDbNull(Dat) OrElse String.IsNullOrEmpty(Dat) then
return false
end if
return true
end function
You can replace the ValidDateTime with an "Iif".
Other example returning max value of Double items, but in the personalized object(MyObject), items are String type.
(From Item as MyObject in MyListOfObject
Let Frec as Double = Convert.ToDouble(Iif(String.IsNullOrEmpty(Item.Frecuency),0,Item.Frecuency))
Let Pot as Double = Convert.ToDouble(Iif(String.IsNullOrEmpty(Item.Power),0,Item.Power))
Let max as Double= {Frec, Pot}.Max
select max).Max
Recap
- Declare Column Items separately. Parse It(If you need)
- Create an Item for store the max value of the row(all columns)
- Create an Array, put in all declared Items and asign array to the before declared item(Item created in step 2)
- Append in the end of the array the Max property like this: {Item1, Item2,...}.Max
- Create Select fr return the Max item
- Append in the end the linq Max property. (MyLinQ).Max
I hope this code helps to someone. Sorry for my English.
Source: stackoverflow.com
Related Articles
- select max between two columns in linq
- LINQ Select mutiply columns as data source for combobox C#
- Linq code to select one item
- Select All columns for all tables in join + linq join
- Select all columns on an object with Linq
- Difference between cast and as inside a select in LINQ
- LINQ Select Dynamic Columns and Values
- Select all columns after JOIN in LINQ
- Linq To SQL Select Dynamic Columns
- Select all columns but group by only one in linq
- IF statement inside a LINQ SELECT to include columns
- Linq to SQL select multiple columns
- difference between select and where in LINQ
- LINQ to SQL Select Distinct by Multiple Columns and return entire entity
- Different results between yield return and LINQ Select
- LINQ Source Code Available
- Select specific columns for Linq group by
- Select only few columns in LINQ query
- linq group by and select multiple columns not in group by
- LINQ to SQL join 3 tables and select multiple columns and also using Sum
- How can I use linq to select the columns of a jagged array
- Is there a difference between select new T() and select new T in LINQ
- Select multiple columns in LINQ
- Select Columns runtime in LINQ LIST<>
- Select multiple columns without join in LINQ
- Linq to Datarow, Select multiple columns as distinct?
- How to select all columns in LINQ Datatable join?
- How to select multiple columns from dataset into a string list with LinQ
- How to concatenate two columns in linq to sql query's select projection
- creating Linq to sqlite dbml from DbLinq source code
- Using GroupBy DateTime with Entity Framework throws an exception
- How to select all columns plus a custom one from table by using EF LINQ?
- Linq to SQL Date conversion error
- C# + EntityFramework: Convert multiple group by query to nested JSON
- Copy specific rows from One DataTable to another DataTable using LINQ
- Parsing Conditional Expressions to String
- using PredicateBuilder inside a select
- How to set "startAt" argument value when using LINQ with Jira Atlassian SDK
- GroupJoin, SelectMany, GroupBy and Sum
- Shaping EF LINQ Query Results Using Multi-Table Includes
- Multiple `Where()` vs. single `Where()`
- Linq to Entities query non primitive type
- Trying to concatenate LINQ column values, Not sure where the LINQ error is?
- The member <some column> is not supported in SubSonic
- Stepping through Linq statement tips
- How to execute a LINQ statement without assigning it to a variable?
- Trouble mapping to DTOs with LINQ
- How to join table in LINQ and return list in MVC
- I Have a problem retrieving some null data from my DB?
- Custom Linq method could not be translated