|
That's some odd code you have there.
For starters, max is a local variable and it is declared inside the foreach loop - so it's scope is limited to that loop: it cannot be accessed outside the loop, so it's pretty much useless.
Then there is the loop itself: that's useless as well, because your method will always return the same value - the last item in the input collection.
Then there is the linq code: why are you sorting your collection each time round the loop? The list can't change inside the loop (or you'd get an "collection modified" error at runtime) so the order is always the same.
Then there is the Max call - that is also useless as it will always return the same value since the collection still can't change inside the loop!
Then there is the error you have noticed: and that's because Max uses IComparable to work out the order of values, and your class TheQFromDB doesn't implement IComparable so Max can't compare any items to have any idea what is the maximum.
Probably what you want is
... input.Max(s => s.SecondsDiff); But that's just guesswork given that I know nothing about your classes or what that code is actually trying to do...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
First of all . i thank you for your Explanations. its more clear now .
In Fact what all i wanted to do is to get the maxValue from the difference among two Columns from SQL
TheQFromDB is holding some properties such as : DateTime AddedToQueue and DateTime LastStatusUpdate and a method to get the differenc between these two properties
i used now this Method and works very well you can indeed give me your opinion back
public static TheQFromDB GetMaxWithLINQ (List<TheQFromDB> input)
{
var max = input.OrderByDescending(s => s.SecondsDiff).First();
return max;
}
|
|
|
|
|
Why not just use Max with SecondsDiff?
Sorting the whole thing just to get the first item is a very inefficient way to get the max value ... and if this is sourced from a DB, you have no idea how many items you may be dealing with in a years time. Sort - even Quick Sort - is a complex operation, "what's the biggest?" is a single pass through the data.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: Why not just use Max with SecondsDiff?
Probably because that would only return the SecondsDiff , not the whole object.
(See my reply to Maciej below[^].)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You get that exception because the TheQFromDB object has no built-in comparer to tell how two of these object compare to each other to determine order.
This code also looks like you're trying to generate your own "next highest" value. If that's the case, this code will not work in a multiuser environment.
|
|
|
|
|
Max() method does not need firstly to use OrderByDescending() method
var max = input.OrderByDescending(s=> s.SecondsDiff).Max();
A proper usage of Max() is:
TheQFromDB GetItem = input.Max(ele=> ele.SecondsDiff);
With this piece of code and no other details i can't help you more...
Good luck!
modified 30-Apr-19 2:31am.
|
|
|
|
|
Maciej Los wrote: So, why do you try to loop through... one element?
Um ... more needed?
TheQFromDB GetItem = input.First();
foreach (var item in input) He doesn't use the value he loads into GetItem.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Well... Good diagnosis. I was in a state of black-out.
|
|
|
|
|
We all get that ... I'm considering an intravenous drip ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Maciej Los wrote: TheQFromDB GetItem = input.Max(ele=> ele.SecondsDiff);
Unfortunately, that won't compile. The Max overload that takes a projection will return the result of that projection, not the item it was projected from.
public static TResult Max<TSource,TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector);
To get the item with the maximum value, you'd need a custom extension method - for example:
MoreEnumerable.MaxBy(TSource, TKey) Method (IEnumerable(TSource), Func(TSource, TKey), IComparer(TKey))[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Are you sure, Richard? Take a look at method definition:
public static TheQFromDB GetMaxWithLINQ (List<TheQFromDB> input)
There's a list of data, not an entity (model) passed as an argument...
|
|
|
|
|
It doesn't make any difference. There is no Max method on the List<T> type, so you're calling the Enumerable.Max extension method[^]. The overload you're calling returns the maximum projected value, not the item from which that value was projected. There is no overload which would return the item with the highest projected value, so you'd have to use a custom extension method.
Try running it yourself:
public class Foo
{
public double SecondsDifference { get; set; }
}
static void Main()
{
List<Foo> foos = new List<Foo>
{
new Foo { SecondsDifference = 0 },
new Foo { SecondsDifference = 42 },
new Foo { SecondsDifference = 1 },
};
var max = foos.Max(f => f.SecondsDifference);
Console.WriteLine(max.GetType());
Console.WriteLine(max);
}
LINQ Max | C# Online Compiler | .NET Fiddle[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Got it!
I was fixed on Max value instead of item with max value.
Thanks.
|
|
|
|
|
Final solution may look like:
public static TheQFromDB GetMaxWithLINQ (List<TheQFromDB> input)
{
return input.OrderByDescending(s=> s.SecondsDiff).First();
}
or:
public static TheQFromDB GetMaxWithLINQ (List<TheQFromDB> input)
{
var max = input.Max(s=> s.SecondsDiff);
return input.Where(s=> s.SecondsDiff==max).First();
}
|
|
|
|
|
hi there
i have got 3 solutions which they are working very well at work so far :
1-
public static TheQFromDB GetMaxWithLINQ (List<TheQFromDB> input)
{
var max = input.OrderByDescending(s => s.SecondsDiff).First();
return max;
}
2-
public static TheQFromDB GetMax2 (List<TheQFromDB> input)
{
TheQFromDB GetItem = input.First();
foreach (var item in input)
{
if (item.SecondsDiff > GetItem.SecondsDiff)
{
GetItem = item;
}
}
return GetItem;
3-
public static TheQFromDB GetMax (List<TheQFromDB> input)
{
if (input == null || input.Count < 1)
return null;
int MaxIndex = input.Count - 1;
for (int CurrentIndex = input.Count -2; CurrentIndex >=0; CurrentIndex--)
{
if (input[MaxIndex].SecondsDiff < input[CurrentIndex].SecondsDiff)
MaxIndex = CurrentIndex;
}
return input[MaxIndex];
}
the number 3 get the index which holding the object back .
|
|
|
|
|
how we find run time between bubble sort and insertion sort in c#
|
|
|
|
|
Depends on the version of VS you are using. In 2019 (and I think 2017) you have performance monitoring tools under the Analyse menu: Measuring app performance using profiling - Visual Studio | Microsoft Docs[^]
For earlier versions the simplest way is to use the Stopwatch class to measure the execution time.
But remember: you will need multiple runs (think thousands, averaged), and multiple test data sets (for wildly different cases and data sizes) to get meaningful results: you will get nothing useful by providing one set of ten random values and profiling it a single time as there are far too many "other factors" in play here, such as JIT compilation, caching, ...
You also need to probably repeat your tests for multiple builds: A Debug build does not get optimised anywhere near as aggressively as a Release build for example.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Most importantly, the performance depends very much on how pre-sorted or random the input is. That is a major reason why there isn't a single "best" algorithm.
|
|
|
|
|
In this Method returns the TheQFromDB an Instance from the input list where the property SecondsDiff is the highest and in each item in input has a SecondDiff value but one is the highest . how could i find that without using LINQ ? and how could i check all the items . Look at the Code please
public static TheQFromDB CalculateMaxVal (List<TheQFromDB> input)
{
double maxValue = double.MinValue;
foreach (var item in input)
{
if (item.SecondsDiff > maxValue)
{
maxValue = item.SecondsDiff;
}
}
Console.WriteLine(maxValue);
return null;
}
|
|
|
|
|
Why aren't you doing it this way?
foreach (var item in input)
{
maxValue = Math.Max(maxValue, item.SecondsDiff);
}
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
hi. i tried its working but i want to return the object back and not the maxvalue !?
|
|
|
|
|
That's not what you asked (this time).
Someone else has already provided you with the correct answer. There is no simpler non-Linq way to do what you want.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 27-Apr-19 6:07am.
|
|
|
|
|
yes you have right . i am getting stuck here with this code which give me back the maxValue that i need but in fact i want the whole object back . is my Query clear ? look at the code please
public static TheQFromDB CalculateMaxVal (List<TheQFromDB> input)
{
double maxValue = double.MinValue;
foreach (var item in input)
{
if (item.SecondsDiff > maxValue)
{
maxValue = item.SecondsDiff;
}
}
Console.WriteLine(maxValue);
return null;
}
|
|
|
|
|
public static TheQFromDB CalculateMaxVal (List<TheQFromDB> input) {
double maxValue = double.MinValue;
TheQFromDb maxQ = null;
foreach (var item in input) {
if (item.SecondsDiff > maxValue) {
maxValue = item.SecondsDiff;
maxQ = item;
}
}
Console.WriteLine(maxValue);
return maxQ;
}
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|