linq.except的" sql-syntax"越多[英] The more "SQL-syntax" of Linq.Except

本文是小编为大家收集整理的关于linq.except的" sql-syntax"越多的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

LINQ中的大多数表达式可以用两种语法写入.
基本上, method-syntax 和 sql样syntax .

例如:

methot-syntax:
var results = MySet.Where(n => n.Status == State.ACTIVE);

sql样syntax:
var results = from n in MySet where n.Status == State.ACTIVE select n;

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

示例:

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;

问题
我称之为的方法是正式的名称 - syntax和类似SQL的syntax吗?
有没有办法在except操作上执行类似SQL的Syntax?

推荐答案

有正式名称我所说的方法形式和类似SQL 语法?

是的,一个是方法语法,另一个是(sql like)是查询语法(Query Sytnax编写的查询表达式).

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

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

请参阅: linq(c#)中的查询语法和方法语法

对于您的其他问题:

是否有一种方法可以在操作除外执行类似SQL的Syntax?

否.我还没有看到Except有查询语法.

请参阅: linq(c#)中的查询语法和方法语法

入门语言集成查询(LINQ)中的大多数查询 文档是通过使用LINQ声明查询语法编写的. 但是,查询语法必须翻译成 .NET通用语言运行时(CLR)编译代码时.这些 方法调用带有名称的标准查询操作员 例如,选择,选择,组,加入,最大和平均.你可以 使用方法语法直接调用它们,而不是查询语法.

因此,似乎您只能使用Where, Select, GroupBy, Join, Max, and Average与查询语法使用.

其他推荐答案

C#中的LINQ语法支持的唯一方法是:

Where      // where
Select     // select / let
SelectMany // from
Join       // join
GroupBy    // group by
GroupJoin  // join into

,甚至不是所有这些过载.对于其他所有内容,您都可以自己称呼这些方法.

使用linq语法执行查询的唯一方法是将Except交换为GroupJoin:

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

var composites = from n in numbers
                 join p in primes on n equals p into grp
                 where !grp.Any()
                 select n;

这可能不值得 - 对于那些对Linq没有牢固掌握的人来说,这将效率较低,并且可能无法立即理解.

f#支持一组更大的查询表达式.希望他们能将其中的一些带到C#.

其他推荐答案

您可以尝试查询和方法语法的组合.

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

var composites = from n in numbers
                 where !(primes.Contains(n))
                 select n;

本文地址:https://www.itbaoku.cn/post/1556786.html

问题描述

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

For example:

Method-syntax:
var results = MySet.Where(n => n.Status == State.ACTIVE);

SQL-like-syntax:
var results = from n in MySet where n.Status == State.ACTIVE select n;

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.

Example:

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;

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?

推荐答案

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

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

LINQ Query Expressions (C# Programming Guide)

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

See: Query Syntax and Method Syntax in LINQ (C#)

For your other question:

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

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

See: Query Syntax and Method Syntax in LINQ (C#)

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.

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

其他推荐答案

The only methods with LINQ syntax support in C# are:

Where      // where
Select     // select / let
SelectMany // from
Join       // join
GroupBy    // group by
GroupJoin  // join into

And not even all the overloads of those. For everything else, you're stuck calling the methods yourself.

The only way to perform your query using LINQ syntax is to swap Except for a GroupJoin:

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

var composites = from n in numbers
                 join p in primes on n equals p into grp
                 where !grp.Any()
                 select n;

This probably isn't worth it, though -- it will be somewhat less efficient and probably not immediately understandable to those who don't have a solid grasp of LINQ.

F# supports a much greater set of query expressions. Wish they'd bring some of it over to C#.

其他推荐答案

You could try a combination of query and method syntax.

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

var composites = from n in numbers
                 where !(primes.Contains(n))
                 select n;