更多的"SQL语法"的Linq.Except [英] The more "SQL-syntax" of Linq.Except

查看:58
本文介绍了更多的"SQL语法"的Linq.Except的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linq中的大多数表达式可以用两种语法编写.
基本上,方法语法类似SQL的语法.

Most expressions in Linq can be written in two syntaxs.
Basically, the Method-syntax and the SQL-like-syntax.

例如:

方法语法:
var results = MySet.Where(n => n.Status == State.ACTIVE);

类似SQL的语法:
var results = from n in MySet where n.Status == State.ACTIVE select n;

我想在类似SQL的语法中使用.Except,但是只能在线找到在方法语法中使用它的示例.

I'd like to use .Except in the SQL-like-syntax, but can only find examples online where it is used in the Method-syntax.

示例:

int[] numbers = Enumerable.Range(1,20).ToArray();
int[] primes = new[] { 2, 3, 5, 7, 11, 13, 17 };

//var composites = from n in numbers select n except primes; // This does not work.
var composites = numbers.Except(primes);                     // This works
return composites;

问题
我所说的方法语法和类似SQL的语法有正式名称吗?
有没有办法对except操作执行类似SQL的语法?

Questions
Are there formal names for what I'm calling Method-syntax and SQL-like-syntax?
Is there a way to do the SQL-like-syntax on the except operation?

推荐答案

我所说的方法形式和类似SQL的形式名称是否存在 语法?

Are there formal names for what I'm calling Method-form and SQL-like syntax?

是的,一种是方法语法,另一种是[em>(类似于SQL)是查询语法(用查询sytnax编写的查询表达式).

Yes one is Method Syntax and the other (SQL like) is query syntax (query expression written in query sytnax).

LINQ查询表达式(C#编程指南)

查询表达式以声明性查询语法编写 在C#3.0中引入.

Query expressions are written in a declarative query syntax introduced in C# 3.0.

请参阅: LINQ(C#)中的查询语法和方法语法

对于其他问题:

是否可以对except操作执行类似SQL的语法?

Is there a way to do the SQL-like-syntax on the except operation?

不.我还没有看到带有查询语法的Except.

No. I haven't seen Except with query syntax.

请参阅: LINQ(C#)中的查询语法和方法语法

入门语言集成查询(LINQ)中的大多数查询 文档是使用LINQ声明性查询语法编写的. 但是,必须将查询语法转换为对 编译代码时的.NET公共语言运行库(CLR).这些 方法调用调用具有名称的标准查询运算符 例如位置,选择,分组依据,联接,最大和平均.你可以 通过使用方法语法而不是查询语法直接调用它们.

Most queries in the introductory Language Integrated Query (LINQ) documentation are written by using the LINQ declarative query syntax. However, the query syntax must be translated into method calls for the .NET common language runtime (CLR) when the code is compiled. These method calls invoke the standard query operators, which have names such as Where, Select, GroupBy, Join, Max, and Average. You can call them directly by using method syntax instead of query syntax.

因此,看来您只能将Where, Select, GroupBy, Join, Max, and Average与查询语法一起使用.

So it appears you can only use Where, Select, GroupBy, Join, Max, and Average with Query Syntax.

这篇关于更多的"SQL语法"的Linq.Except的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆