22

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?

Legion
  • 3,922
  • 8
  • 51
  • 95
  • 2
    I wonder if that's something that should be fixed, since it's not more efficient: http://stackoverflow.com/questions/8663897/why-is-linq-wherepredicate-first-faster-than-firstpredicate – Garrison Neely Jun 10 '13 at 21:49

4 Answers4

50

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);
Lee
  • 142,018
  • 20
  • 234
  • 287
9
var user = db.Users.Single(u => u.UserID == userID)

Syntactic sugar

AD.Net
  • 13,352
  • 2
  • 28
  • 47
5

A bunch of the Linq expressions, Enumerable Methods, work this way, like the method Single, which takes a predicate and returns a true only if the condition is satisfied (test passed), otherwise a false.

These should be used instead of a Where() and Single() together:

var user = db.Users.Single(u => u.UserID == userID); 
// Checks for if there is one and, only one element that satisfy the condition.

and

var user = db.Users.Any(u => u.UserID == userID);  
// Checks for if there are any elements that satisfy the condition.

and

var user = db.Users.All(u => u.UserID == userID);  
// Checks if all elements satisfy the condition.
Craig Gjerdingen
  • 1,844
  • 1
  • 21
  • 21
0

Select * from where field in (list, of,field) can be implemented in Linq Lambda using two approaches

  1. Using "Contains"
  2. Using "Join"

Code sample here in DotNetFiddle

enter image description here

using System;
 using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {

    //Initializing a collection of 4 Employees fromthree Different departments <Name of Employee, Dept ID>
    List<KeyValuePair<string, int>> EmployeeDept = new List<KeyValuePair<string, int>> {
        new KeyValuePair<string, int> ("Gates",2),
        new KeyValuePair<string, int> ("Nadella",2),
            new KeyValuePair<string, int> ("Mark",3),
        new KeyValuePair<string, int> ("Pichai",4)
    };

    //Filter the Employees belongs to these department
    int[] deptToFilter ={3,4};

    //Approach 1 - Using COntains Method
    Console.WriteLine ("Approach 1 - Using Contains");
    Console.WriteLine ("==========================================");   
    var filterdByContains = EmployeeDept.Where(emp => deptToFilter.Contains(emp.Value));
    foreach (var dept3and4employees in filterdByContains)
    {
        Console.WriteLine(dept3and4employees.Key+" - "+dept3and4employees.Value);
    }

    //Approach 2 Using Join
    Console.WriteLine ("\n\nApproach 2 - Using Join");
    Console.WriteLine ("==========================================");   
    var filteredByJoin = EmployeeDept.Join(
        deptToFilter,
        empdept => empdept.Value, 
        filterdept => filterdept, 
        (empdep,filddep) => new KeyValuePair<string, int>(empdep.Key, empdep.Value)
    );

    foreach (var dept3and4employees in filteredByJoin)
    {
        Console.WriteLine(dept3and4employees.Key+" - "+dept3and4employees.Value);
    }

}
}
Venkatesh Muniyandi
  • 5,132
  • 2
  • 37
  • 40