英文:
Linq Where query without lambda
问题
Lambda 是一个有用的东西,但对我来说有点令人困惑。能否有人不使用 lambda 来执行 LINQ 的 'WHERE' 查询,这样我就能理解发生了什么。
List<Student> st = new List<Student>() {
new Student(){Id=1,Name="Nav"},
new Student(){Id=2,Name="San"},
new Student(){Id=3,Name="Jat"},
};
Student? me = st.Where(st => st.Name == "Nav").FirstOrDefault();
我想了解如何在不使用 lambda 表达式的情况下执行这个操作。
英文:
Lambda is a useful thing but also a bit confusing for me. can someone please do the linq'WHERE' query without lambda so i can understand whats been happening.
List<Student> st = new List<Student>() {
new Student(){Id=1,Name="Nav"},
new Student(){Id=2,Name="San"},
new Student(){Id=3,Name="Jat"},
};
Student? me = st.Where(st => st.Name == "Nav").FirstOrDefault();
I wanna see how to do this without lambda.
答案1
得分: 6
首先,Lambda表达式是.net
和特别是LINQ
中非常强大和常见的工具,因此学习如何阅读和理解它们非常有用。C#中的Lambda表达式允许您编写简洁和富有表现力的代码,使您的代码更易阅读和理解。
在您的代码中,我们使用List<T>
类的Where
方法以及Lambda表达式来过滤学生列表,并获取Name
属性匹配"Nav"
的学生。
以下是一个示例,展示了如何在不使用Lambda表达式和LINQ的情况下实现相同的结果:
var st = new List<Student>() {
new Student(){ Id = 1, Name = "Nav" },
new Student(){ Id = 2, Name = "San" },
new Student(){ Id = 3, Name = "Jat" },
};
Student firstOrDefaultStudent = null;
foreach (Student s in st)
{
// "if"相当于"where"
// 我们比较每个学生的Name属性与字符串"Nav"。
// 当找到Name属性匹配"Nav"的学生时,
// 将该学生赋给变量"firstOrDefaultStudent"
if (s.Name == "Nav")
{
firstOrDefaultStudent = s;
// 这相当于"first or default"
// 当找到第一个匹配的值时,我们停止循环
// 因为我们只需要第一个满足条件的学生
break;
}
}
您还可以用方法替代Lambda表达式。这个方法将针对集合中的每个项进行调用,并返回一个bool
值,表示项是否符合方法中描述的条件。
var st = new List<Student>() {
new Student(){ Id = 1, Name = "Nav" },
new Student(){ Id = 2, Name = "San" },
new Student(){ Id = 3, Name = "Jat" },
};
Student? me = st.Where(IsNameEqualsToNav).FirstOrDefault();
private static bool IsNameEqualsToNav(Student st)
{
return st.Name == "Nav";
}
请记住LINQ的另一个重要行为,即LINQ操作通常使用延迟执行:
延迟执行意味着延迟对表达式的评估,直到实际需要其实现值。延迟执行在必须操作大型数据集的情况下,尤其在包含一系列链接查询或操作的程序中,可以极大地提高性能。在最佳情况下,延迟执行仅允许通过源集合进行单次迭代。
LINQ技术广泛使用延迟执行,既包括核心System.Linq类的成员,也包括各种LINQ命名空间中的扩展方法,如System.Xml.Linq.Extensions。
英文:
First of all, lambdas are a very powerful and common tool in .net
and LINQ
in particular, so it is very useful to learn how to read and understand them. Lambda expressions in C# allow you to write concise and expressive code that can make your code more readable and easier to understand.
In your code, we use the Where
method of the List<T>
class along with a lambda expression to filter the list of students and get the student whose Name
property matches "Nav"
.
here's an example of how to achieve the same result without using lambda expressions and LINQ at all:
var st = new List<Student>() {
new Student(){ Id = 1, Name = "Nav" },
new Student(){ Id = 2, Name = "San" },
new Student(){ Id = 3, Name = "Jat" },
};
Student firstOrDefaultStudent = null;
foreach (Student s in st)
{
// if is analog for where
// we compare the Name property of each student with the string "Nav".
// When we find a student whose Name property matches "Nav",
// we assign that student to the variable "firstOrDefaultStudent"
if (s.Name == "Nav")
{
firstOrDefaultStudent = s;
// this is analog for first or default
// when we find first matched value we stop our loop
// because we need only first student that matches condition
break;
}
}
Also you can replace your lambda with method. This method will be called for each item in the collection and return a bool
value, which means whether the item fits the condition described in the method or not.
var st = new List<Student>() {
new Student(){ Id = 1, Name = "Nav" },
new Student(){ Id = 2, Name = "San" },
new Student(){ Id = 3, Name = "Jat" },
};
Student? me = st.Where(IsNameEqualsToNav).FirstOrDefault();
private static bool IsNameEqualsToNav(Student st)
{
return st.Name == "Nav";
}
Please remember about another important LINQ behavior. LINQ operations are often implemented to use deferred execution:
> Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required. Deferred execution can greatly improve performance when you have to manipulate large data collections, especially in programs that contain a series of chained queries or manipulations. In the best case, deferred execution enables only a single iteration through the source collection.
> The LINQ technologies make extensive use of deferred execution in both the members of core System.Linq classes and in the extension methods in the various LINQ namespaces, such as System.Xml.Linq.Extensions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论