本文介绍了更多的"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?

推荐答案

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

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

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

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

对于其他问题:

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

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

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

因此,看来您只能将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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 04:40