Reputation: 509
This might be very simple but I am not able to see data after converting
public void SearchGrid(int id, ObservableCollection<TestModel> msgField)
{
foreach (var c in msgField)
DisplayColor = c.Id == id ? Brushes.Blue : Brushes.Black;
}
Linq:
Enumerable.FirstOrDefault(msgField.Where(x => x.Id == id), x => { DisplayColor = Brushes.CadetBlue; });
Upvotes: 0
Views: 55
Reputation: 564323
In general, it's a bad idea to create queries that produce side effects. I would personally rewrite this as:
DisplayColor = msgField.Any(x => x.Id == id) ? Brushes.Blue : Brushes.Black;
This produces the output I believe you are attempting to generate. It will set the DisplayColor
to blue or black based on whether there is a matching "msgField" element.
To get the same result as your current looping code, you could use:
if (msgField.Any())
DisplayColor = msgField.Last().Id == id ? Brushes.Blue : Brushes.Black;
This will produce the same output as your loop, as your loop will overwrite DisplayColor
with each iteration, so only the last item matters.
Upvotes: 5