2013-06-10 15 views
15

Używając LINQ z pojedynczym(), zawsze podkreślam zieloną linię kodu z sugestią "Zastąp pojedyncze połączenie pojedynczym". Co to znaczy? Oto przykład linii kodu, która powoduje tę sugestię:Co znaczy "Zamień na pojedyncze połączenie na pojedyncze"?

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

Jak widać, używam tylko Single() raz. Więc ... o co chodzi?

+2

Zastanawiam się, czy to jest coś, co powinno być stałe, ponieważ nie jest bardziej wydajny: http://stackoverflow.com/questions/8663897/why-is-linq-wherepredicate-first-faster-than-firstpredicate –

Odpowiedz

37

Zakładam, to znaczy, użyj overload of Single która przyjmuje orzeczenie zamiast korzystania Where i Single razem:

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

Nice! Pomogło mi to również, gdy ReSharper podał podobną sugestię dotyczącą FirstOrDefault(). –

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

cukier syntaktyczny

+0

Mniej kodu oznacza mniejsze szanse na literówki;) – Evvo

3

A kilka wyrażeń LINQ, Enumerable Methods, praca w ten sposób, podobnie jak metoda Single, która pobiera predykat i zwraca wartość true tylko wtedy, gdy warunek jest spełniony (test przeszedł pomyślnie), w przeciwnym razie jest to fałsz.

te powinny być stosowane zamiast Gdzie() oraz pojedynczy() razem:

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

i

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

i

var user = db.Users.All(u => u.UserID == userID); 
// Checks if all elements satisfy the condition. 
0

select * from gdzie polu (list, of, field) może być zaimplementowana w Linq Lambda przy użyciu dwóch podejść

  1. Używanie "Zawiera"
  2. Używanie "Join"

przykładowy kod here w 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); 
    } 

} 
} 
Powiązane problemy