C# 什么是;替换为对“single”的单次调用;什么意思?
在将LINQ与Single()一起使用时,我的代码行始终以绿色下划线,并建议“替换为Single调用Single”。这是什么意思?下面是一个导致该建议的代码行示例:C# 什么是;替换为对“single”的单次调用;什么意思?,c#,.net,linq,C#,.net,Linq,在将LINQ与Single()一起使用时,我的代码行始终以绿色下划线,并建议“替换为Single调用Single”。这是什么意思?下面是一个导致该建议的代码行示例: var user = db.Users.Where(u => u.UserID == userID).Single(); 正如您所看到的,我只使用了Single()一次。所以这是怎么回事?我想这意味着,使用which作为谓词,而不是同时使用Where和Single: var user = db.Users.Single(u
var user = db.Users.Where(u => u.UserID == userID).Single();
正如您所看到的,我只使用了Single()一次。所以这是怎么回事?我想这意味着,使用which作为谓词,而不是同时使用
Where
和Single
:
var user = db.Users.Single(u => u.UserID == userID);
var user = db.Users.Single(u => u.UserID == userID);
// Checks for if there is one and, only one element that satisfy the condition.
语法sugar一组Linq表达式以这种方式工作,就像方法Single一样,它接受一个谓词,只有在满足条件(测试通过)时才返回true,否则返回false 应使用这些选项,而不是同时使用Where()和Single()
var user = db.Users.Single(u => u.UserID == userID);
var user = db.Users.Single(u => u.UserID == userID);
// Checks for if there is one and, only one element that satisfy the condition.
及
及
选择*从何处可以使用两种方法在Linq Lambda中实现字段in(list,of,field)
使用系统;
使用System.Linq;
使用系统集合;
使用System.Collections.Generic;
公共课程
{
公共静态void Main()
{
//初始化来自三个不同部门的4名员工的集合
List EmployeeDept=新列表{
新的KeyValuePair(“门”,2),
新的KeyValuePair(“Nadella”,2),
新的KeyValuePair(“标记”,3),
新的KeyValuePair(“Pichai”,4)
};
//筛选属于这些部门的员工
int[]deptotofilter={3,4};
//方法1-使用COntains方法
Console.WriteLine(“方法1-使用包含”);
Console.WriteLine(“===================================================================”);
var filterdByContains=EmployeeDept.Where(emp=>deptofilter.Contains(emp.Value));
foreach(FilterByContains中的var Dept3和4员工)
{
Console.WriteLine(Dept3和4Employees.Key+“-”+Dept3和4Employees.Value);
}
//方法2使用Join
Console.WriteLine(“\n\n路径2-使用连接”);
Console.WriteLine(“===================================================================”);
var filteredByJoin=EmployeeDept.Join(
德普托过滤器,
empdept=>empdept.值,
filterdept=>filterdept,
(empdep,fildep)=>新的KeyValuePair(empdep.Key,empdep.Value)
);
foreach(var Dept3和filteredByJoin中的4名员工)
{
Console.WriteLine(Dept3和4Employees.Key+“-”+Dept3和4Employees.Value);
}
}
}
我想知道这是否是应该解决的问题,因为它效率不高:太好了!当ReSharper对FirstOrDefault()给出类似建议时,这也对我有所帮助。
var user = db.Users.All(u => u.UserID == userID);
// Checks if all elements satisfy the condition.
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);
}
}
}