C# 带有别名的 Linq
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15906967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq with alias
提问by Jean Paul Olvera
I have the following line in c#:
我在 C# 中有以下行:
var name = (from x in db.authors
where fullName == "Jean Paul Olvera"
orderby x.surname
select new { x.id_author, fullName= String.Concat(x.name," ", x.surname) });
my problem is I want to use the alias in my where clause, but I can't, 'fullName' appears as not declared.
我的问题是我想在我的 where 子句中使用别名,但我不能,'fullName' 显示为未声明。
采纳答案by cdhowie
You can use let
to create intermediate values:
您可以使用let
创建中间值:
var name = (from x in db.authors
let fullName = x.name + " " + x.surname
where fullName == "Jean Paul Olvera"
orderby x.surname
select new { x.id_author, fullName });
回答by Reed Copsey
You haven't created it yet.
你还没有创建它。
That being said, since you know the name in advance, you should be able to perform the query on the parts:
话虽如此,既然您事先知道名称,您应该能够对零件执行查询:
var name = from x in db.authors
where name == "Jean Paul" && surname == "Olvera"
orderby x.surname
select new { x.id_author, fullName= String.Concat(x.name," ", x.surname) };
Otherwise, you can use let
to do this:
否则,您可以使用以下let
方法执行此操作:
var name = from x in db.authors
let fullName = String.Concat(x.name," ", x.surname)
where fullName == "Jean Paul Olvera"
orderby x.surname
select new { x.id_author, fullName=fullName ) };
回答by Jon Skeet
You need to put that part of the projection earlier, which is easy with a let
clause:
您需要提前放置投影的那部分,这很容易使用let
子句:
var name = from x in db.authors
let fullName = x.name + " " + x.surname
where fullName == "Jean Paul Olvera"
orderby x.surname
select new { x.id_author, fullName };
Note that x.name + " " + x.surname
will be compiled to the same code as String.Concat(x.name, " ", x.surname)
, but is more readable to most people. Also note that as you're not doing anything outside the ()
parentheses, there's no need for them.
请注意,x.name + " " + x.surname
将被编译为与 相同的代码String.Concat(x.name, " ", x.surname)
,但对大多数人来说更具可读性。另请注意,由于您没有在()
括号外执行任何操作,因此不需要它们。
I would hopethat any good SQL LINQ provider should turn this query into a sensible and efficient SQL query, but you should validate this yourself. On the other hand, I would generallysuggest preferring querying over individual fields, e.g.
我希望任何好的 SQL LINQ 提供程序都应该把这个查询变成一个明智和高效的 SQL 查询,但你应该自己验证这一点。另一方面,我通常建议优先查询单个字段,例如
where x.name == "Jean Paul" && x.surname == "Olvera"
回答by D Stanley
use the let
clause:
使用let
条款:
var name = (from x in db.authors
let fullName = String.Concat(x.name," ", x.surname)
where fullname = "Jean Paul Olvera"
orderby x.surname
select new { x.id_author, fullName });
回答by Servy
It's easier in the method syntax, as you aren't constrained to the order of the operations:
方法语法更容易,因为您不受操作顺序的限制:
var query = authors.OrderBy(x => x.surname)
.Select(x => new
{
x.id_author,
fullName = String.Concat(x.name, " ", x.surname)
})
.Where(x => x.fullName == "Jean Paul Olvera");
回答by hadiz7
linq1DataContext ll = new linq1DataContext();
if (comboBox1.SelectedIndex == 0)
{
var q = from m in ll.personals
let ??= m.id
let ??? = m.name
select new {
??,
???,
};
dataGridView1.DataSource = q;
}