Linq Where查询无lambda

huangapple go评论55阅读模式
英文:

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&lt;Student&gt; st = new List&lt;Student&gt;() {
              new Student(){Id=1,Name=&quot;Nav&quot;},
              new Student(){Id=2,Name=&quot;San&quot;},
              new Student(){Id=3,Name=&quot;Jat&quot;},
        };
                Student? me = st.Where(st =&gt; st.Name == &quot;Nav&quot;).FirstOrDefault();

I wanna see how to do this without lambda.

答案1

得分: 6

首先,Lambda表达式是.net和特别是LINQ中非常强大和常见的工具,因此学习如何阅读和理解它们非常有用。C#中的Lambda表达式允许您编写简洁和富有表现力的代码,使您的代码更易阅读和理解。

在您的代码中,我们使用List&lt;T&gt;类的Where方法以及Lambda表达式来过滤学生列表,并获取Name属性匹配&quot;Nav&quot;的学生。

以下是一个示例,展示了如何在不使用Lambda表达式和LINQ的情况下实现相同的结果:

var st = new List&lt;Student&gt;() {
   new Student(){ Id = 1, Name = &quot;Nav&quot; },
   new Student(){ Id = 2, Name = &quot;San&quot; },
   new Student(){ Id = 3, Name = &quot;Jat&quot; },
};

Student firstOrDefaultStudent = null;

foreach (Student s in st)
{
    // "if"相当于"where"
    // 我们比较每个学生的Name属性与字符串&quot;Nav&quot;。
    // 当找到Name属性匹配&quot;Nav&quot;的学生时,
    // 将该学生赋给变量&quot;firstOrDefaultStudent&quot;
    if (s.Name == &quot;Nav&quot;)
    {
        firstOrDefaultStudent = s;
        // 这相当于"first or default"
        // 当找到第一个匹配的值时,我们停止循环
        // 因为我们只需要第一个满足条件的学生
        break;
    }
}

您还可以用方法替代Lambda表达式。这个方法将针对集合中的每个项进行调用,并返回一个bool值,表示项是否符合方法中描述的条件。

var st = new List&lt;Student&gt;() {
   new Student(){ Id = 1, Name = &quot;Nav&quot; },
   new Student(){ Id = 2, Name = &quot;San&quot; },
   new Student(){ Id = 3, Name = &quot;Jat&quot; },
};

Student? me = st.Where(IsNameEqualsToNav).FirstOrDefault();

private static bool IsNameEqualsToNav(Student st)
{
    return st.Name == &quot;Nav&quot;;
}

请记住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&lt;T&gt; class along with a lambda expression to filter the list of students and get the student whose Name property matches &quot;Nav&quot;.

here's an example of how to achieve the same result without using lambda expressions and LINQ at all:

var st = new List&lt;Student&gt;() {
   new Student(){ Id = 1, Name = &quot;Nav&quot; },
   new Student(){ Id = 2, Name = &quot;San&quot; },
   new Student(){ Id = 3, Name = &quot;Jat&quot; },
};

Student firstOrDefaultStudent = null;

foreach (Student s in st)
{
    // if is analog for where
    // we compare the Name property of each student with the string &quot;Nav&quot;. 
    // When we find a student whose Name property matches &quot;Nav&quot;, 
    // we assign that student to the variable &quot;firstOrDefaultStudent&quot;
    if (s.Name == &quot;Nav&quot;)
    {
        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&lt;Student&gt;() {
   new Student(){ Id = 1, Name = &quot;Nav&quot; },
   new Student(){ Id = 2, Name = &quot;San&quot; },
   new Student(){ Id = 3, Name = &quot;Jat&quot; },
};

Student? me = st.Where(IsNameEqualsToNav).FirstOrDefault();

private static bool IsNameEqualsToNav(Student st)
{
    return st.Name == &quot;Nav&quot;;
}

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.

huangapple
  • 本文由 发表于 2023年2月26日 22:06:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定