Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
var approve = UnitOfWork.Query.Lexis.ApprovedLexis2(DialogService.User.UserID, List.Where(x=>x.Check).Max(x=>x.TxnDate), _batch);
what does line of code returns? can someone explains that? TYIA
List.Where(x=>x.Check).Max(x=>x.TxnDate)
Presumably here List is a list/collection/some IEnumerable<T> for a type T that has (at least) a bool member named Check, and some other member TxnDate (presumably a DateTime of the transaction date).
The Where applies a predicate filter, i.e. it creates a filtered sequence of the items where Check is true. The Max finds the greatest (in terms of x>y, implemented by IComparable[<T>]) of the .TxnDate of each item in the filtered sequence.
So: the expression returns the greatest (last, timewise) transaction date of all the "checked" items in List. If there are no "checked" items, it will throw an exception (you can't ask for the largest of no values; or rather, you can ask, but you won't get a sensible answer).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How to return a person's name when we search for a hobby and it should return empty when there are no matching hobbies.
There are many ways to skin this cat. Using Linq is probably the most standard.
The operation you want to do is to select the Key of the dictionary where the value (the array of hobbies) contains the string "Yoga"
val keys = hobbies.Where(keyvalue => keyvalue.contains("Yoga")).Select(keyvalue => keyvalue.key);
now you have a sequence of keys where one of the hobbies is Yoga.
If there is only a single one, and it's an error if there are more, get CC with keys.Single().
If there could be zero or more, and you just want an arbitrary one, you want keys.FirstOrDefault() which returns null if there isn't one.
In between you have SingleOrDefault() for 0 or 1, or First() for 1 or more select an arbitrary one.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
The following code
double avg = item?.TechnicianTasks?.Average(x => x.Rating) ?? 0
throws
InvalidOperationException: Sequence contains no elements
the item.TechnicianTasks was supposed to be null, however I saw that it is an empty list, however why wouldn't the average just be zero? I'm not understanding the exception.
Not much to it - it's just how the code was written. Take a look at the docs and you'll see -
Exceptions
ArgumentNullException - source is null.
InvalidOperationException - source contains no elements.
Makes sense though. You can't get an average from zero items.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why does Random.Next() have Next in it's name? I know what it does, but the name doesn't seem to correspond to it.
It returns the next number in the infinite sequence of numbers generated from your Random instance's seed.
In computer science jargon, a "generator" is a specific kind of function: one that returns a different result each time it is called. It is traditional to call this function something like next(), because they are often used to return the next piece of a sequence (perhaps infinite). RNGs are just a special case of generator function, returning the next value in a calculated sequence.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to return value from list or a array without brackets?
For example i have List of integers:
List<int> myIntegers = new List<int>();
myIntegers.Add(1);
myIntegers.Add(2);
myIntegers.Add(3);
int newValue = myIntegers; //normally i should use myIntegers[0]
i want myIntegers to return the first value from list but i want to use it without brackets. Is there a way?
Thanks. M.
As you wrote it, it will never compile. However, if you really have a thing against [], then yes. You can use First() (or its companion, FirstOrDefault()), or just ElementAt(0).
First and FirstOrDefault can take a predicate, which is often useful, FirstOrDefault returns default(T) if there is no first element that matches the predicate, and ElementAt does the exact same thing as the [].
If you subclass List<T>, you could potentially do this with an implicit conversion to T. Although, what you are describing is sort of a weird data structure, and not really idiomatic c#. Probably best not to call it a list.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this two list here:
List<KeyValuePair<TextBox, KeyValuePair<string, Type>>> textbox1
So, i need that to get the Textbox i need to write:
textbox1.Key
What should i type to get the KeyValuePair<string, Type>> Type, like textbox1.Value.Value?
The type contains string or int.What i need is to access it's value so i can assign an if operator but i don't know how to. Then i need to modify it but that's my next step and i can arrange that myself.
I suppose textbox1.Key won´t compile since textbox1 is a List and therefor does not have any member called Key. Having said this the following may work for you:
textbox[i].Value.Value
Where i is the index of your KV-pair within the list (rather then the actual key)