3

i have dateSend Column and dateEdit Column in product table, i can select max dateSend with this code:

(from pro in Products.ToList() select new { pro.DateSend }).Max(); 

but i have max between dateSend and dateEdit, please help me.

2
  • What is the type of DateSend? From your remark below I assume it is DateTime?... Is that correct? Commented Mar 27, 2013 at 22:28
  • DateSend = Datetime & DateEdit = Datetime? Commented Mar 27, 2013 at 22:31

4 Answers 4

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; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Error: Nullable object must have a value. because, dateEdit is empty
i use this code: let max = pro.DateEdit != null && pro.DateSend < pro.DateEdit ? pro.DateEdit.Value : pro.DateSend
8

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); 

1 Comment

Error: Nullable object must have a value. because, dateEdit is empty
1
((from pro in Products.ToList() select pro.DateSend).Union( from pro2 in Products.ToList() select pro.DateEdit )).Max(); 

2 Comments

Why scan the collection twice when you can easily select the higher of the two columns for each item?
You're right, it's not the most optimal way. Your answer is indeed a better way of doing it. Added my vote. Cheers mate!
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

  1. Declare Column Items separately. Parse It(If you need)
  2. Create an Item for store the max value of the row(all columns)
  3. Create an Array, put in all declared Items and asign array to the before declared item(Item created in step 2)
  4. Append in the end of the array the Max property like this: {Item1, Item2,...}.Max
  5. Create Select fr return the Max item
  6. Append in the end the linq Max property. (MyLinQ).Max

I hope this code helps to someone. Sorry for my English.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.