|
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
|
|
|
|
|
could someone explain with an Ex why and how should use out ?
what is the benefit to use it ?
|
|
|
|
|
out is a keyword which says that a method parameter will be changed in the method, but that it is not given an initial value - so any value in the "outside world" will not be passed into the method, but the outside world variable will have the new value after the method returns.
When you pass a "normal" parameter, it is passed by value: a copy of the value in the variable is given to the method, and changes will not be reflected in the outside world:
private void DoIt(int x)
{
x = x * 2;
Console.WriteLine(x);
}
...
int x = 100;
Console.WriteLine(x);
DoIt(x);
Console.WriteLine(x); Will print
100
200
100 You can pass a value by reference, and the actual outside world value is used in the method:
private void DoIt(ref int x)
{
x = x * 2;
Console.WriteLine(x);
}
...
int x = 100;
Console.WriteLine(x);
DoIt(ref x);
Console.WriteLine(x); Will print
100
200
200 Or, you could give it as an out parameter:
private void DoIt(out int x)
{
x = x * 2;
Console.WriteLine(x);
}
...
int x = 100;
Console.WriteLine(out x);
DoIt(out x);
Console.WriteLine(x); And you will get compiler errors because x has no initial value inside the DoIt method.
So you change your code:
private void DoIt(out int x)
{
x = 200;
Console.WriteLine(x);
}
...
int x = 100;
Console.WriteLine(x);
DoIt(out x);
Console.WriteLine(x); Will print
100
200
200
It's not used that much, but it's a way to "return multiple values" from a method if you need to.
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!
|
|
|
|
|
thanks alot for the answer . it helped me
since i am a new user here how can i vote for your answer as helped or top answer or something like this .
|
|
|
|
|
If you want to, then open the answer and you will see a green and red arrowhead at the top left. Green is "Good", red is "Bad".
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!
|
|
|
|
|
Hi all;
Imagine I have defined 3 variables as follow:
String x1="Hello";
String x27="Hi";
String x99="Hey";
Then later in the program code I want to access the content of them by making their name.
Int i=27;
Messagebox.show("x" + i.tosting());
"x" + i.tosting() = "New";
What's the correct code in C#? Any idea?
Thank you in advance.
Rara
|
|
|
|