需要使用集合操作吗?

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

Is a C# Query needed to use the set operations?

问题

当我在寻找获取两个列表之间差异的方法时,我找到了这个页面:Set operations (C#)

我发现我可以使用Except方法。但是,这个代码示例中让我感到困惑的是:

string[] planets1 = { "Mercury", "Venus", "Earth", "Jupiter" };
string[] planets2 = { "Mercury", "Earth", "Mars", "Jupiter" };

IEnumerable<string> query = from planet in planets1.Except(planets2)
                            select planet;

foreach (var str in query)
{
    Console.WriteLine(str);
}

/* This code produces the following output:
 *
 * Venus
 */

是这一行:IEnumerable<string> query = from planet in planets1.Except(planets2) select planet;

考虑到我对C#非常陌生,我的问题是,你需要使用这个查询来使用Except方法吗?这一行似乎做了同样的事情:IEnumerable<string> query = planets1.Except(planets2);

我有什么遗漏吗?

英文:

When I was searching for a way to get the difference between two lists I came across this page: Set operations (C#).

I found out, that I can use the Except method. But what confused me in this code example:

string[] planets1 = { "Mercury", "Venus", "Earth", "Jupiter" };
string[] planets2 = { "Mercury", "Earth", "Mars", "Jupiter" };

IEnumerable<string> query = from planet in planets1.Except(planets2)
                            select planet;

foreach (var str in query)
{
    Console.WriteLine(str);
}

/* This code produces the following output:
 *
 * Venus
 */

was this line: IEnumerable<string> query = from planet in planets1.Except(planets2) select planet;

Considering that I’m very new to C#, my question is, do you need this query to use the Except method? This line seems to do the same thing: IEnumerable<string> query = planets1.Except(planets2);

Am I missing something here?

答案1

得分: 2

  1. 使用两种方式编写 LINQ 查询
  2. 查询表达式形式如下
IEnumerable<string> query = from planet in planets1.Except(planets2)
                            select planet;
  1. 方法链形式如下
IEnumerable<string> query = planets1.Except(planets2)
                                .Select(planet => planet);

在查询表达式形式中,必须存在 group 子句或 select 子句,但在方法链形式中,不需要以 select 方法结束。

因此,这两种代码都返回 IEnumerable<string[]>

planets1.Except(planets2)

planets1.Except(planets2).Select(planet => planet)
英文:

You can write LINQ in two way

  1. query expression like form

    IEnumerable&lt;string&gt; query = from planet in planets1.Except(planets2)
                                 select planet;
    
  2. Method chain form

    IEnumerable&lt;string&gt; query = planets1.Except(planets2)
                                     .Select(planet =&gt; planet);
    

In query expression form, group clause or select clause must exists but in method chain form you do not need to end with select method.

So both of these code return IEnumerable&lt;string[]&gt;

planets1.Except(planets2)

and

planets1.Except(planets2).Select(planet =&gt; planet)

huangapple
  • 本文由 发表于 2023年6月11日 20:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450488.html
匿名

发表评论

匿名网友

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

确定