|
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
|
|
|
|
|
hey man . i thank you very much
it works very well
|
|
|
|
|
|
Same as the last time you posted this:
Re: How to get an Not Empty Instance back ? - C# Discussion Boards[^]
You've found the item with the highest value; you just need to print that value:
public static TheQFromDB CalculateMaxVal(IEnumerable<TheQFromDB> input)
{
TheQFromDB result = null;
foreach (var item in input)
{
if (result == null || item.SecondsDiff > result.SecondsDiff)
{
result = item;
}
}
Console.WriteLine(result?.SecondsDiff ?? double.MinValue);
return result;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
public static TheQFromDB CalculateMaxVal(List<TheQFromDB> input)
{
TheQFromDB Q = new TheQFromDB();
double maxValue = double.MinValue;
foreach(var i in input)
{
if (i.SecondsDiff > maxValue)
maxValue = i.SecondsDiff;
}
Console.WriteLine(maxValue);
return Q;
in this Ex here i have a List from Type TheQFromDB and insaid (input) several properties one of them is SecondsDiff .. i want get the class TheQfromDB back but with this code iam getting an Empty instance which call here Q .
|
|
|
|
|
Try:
public static TheQFromDB CalculateMaxVal(IEnumerable<TheQFromDB> input)
{
TheQFromDB result = null;
foreach (var item in input)
{
if (result == null || item.SecondsDiff > result.SecondsDiff)
{
result = item;
}
}
return result;
}
NB: If there are no items in the input sequence, this will return null . If you want it to return an empty instance instead, change the last line to:
return result ?? new TheQFromDB();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|