I am trying to access a user object in a collection with the id = to users101 and set this to another users.
Controller.MyObject.SingleOrDefault(x => x.Id == "user101") = OtherUser();
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can’t do it with one LINQ expression.
Usually LINQ extensions works on enumerables, if
MyObjectis a collection you first have to find the required item and then overwrite it with the new object (moreoverSingleOrDefault()will simply return null if condition is not satisfied).You should write something like this (exact code depends on what
MyObjectis):Please note that if you do not really need the check performed by
SingleOrDefault()you can simplify the code (and avoid the double search performed inSingleOrDefault()andIndexOf()).If this is “performance critical” maybe it is better to write an ad-hoc implementation that does this task in one single pass.