Legion Legion - 1 year ago 154
C# Question

What does "Replace with single call to single" mean?

When using LINQ with Single() I always get my line of code underlined in green with the suggestion "Replace with single call to single." What does this mean? Here's an example of a line of code that results in that suggestion:

var user = db.Users.Where(u => u.UserID == userID).Single();

As you can see I'm only using Single() once. So... what's the deal?

Lee Lee
Answer Source

I assume it means, use the overload of Single which takes a predicate instead of using Where and Single together:

var user = db.Users.Single(u => u.UserID == userID);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download