I am updating the property of the list
items.
class Response
{
public string Name { get; set; }
public int Order { get; set; }
}
Here I want to update the Order
of a List<Response>
variable. As of now, I am looping through each item of the list and updating it.
List<Response> data = FromDb();
foreach (var item in data)
{
if(item.Name.Equals("A"))
{
item.Order=1;
}
if(item.Name.Equals("B"))
{
item.Order=2;
}
//Like this I have arround 20 conditions
}
The above code is working fine, but the problem is the Cognitive Complexity
of the method is more than the allowed.
I tried something like below
data.FirstOrDefault(x => x..Equals("A")).Order = 1;
data.FirstOrDefault(x => x..Equals("B")).Order = 2;
//and more ...
In this code also null
check is not in place, So if the searching string is not present in the list then again it will break.
If I add null
check condition then again the complexity
getting higher.
So here I want without any for loop
or if
, If I can update the Order of the list by using linq
/lamda
or anything else.