C# ?: LINQ 查询中的运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/282791/
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
?: Operator in LINQ Query
提问by tsilb
How do I utilize a ?: operator in the SELECT clause of a LINQ query? If this can't be done, how can I emulate one? The goal is to get a CASE block in my select clause. As you might suspect, I'm getting an error: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
Is this the proper way, or a sufficient way, to say "from a inner join i on a.ipid=i.id inner join u on i.uid=u.id"? If not, please provide one. Thanks.
var query = from a in db.tblActivities from i in db.tblIPs from u in db.tblUsers select new { u.UserName == null ? i.Address : u.UserName, a.Request, a.DateTime };
如何在 LINQ 查询的 SELECT 子句中使用 ?: 运算符?如果这不能做到,我怎么能效仿呢?目标是在我的 select 子句中获得一个 CASE 块。正如您可能怀疑的那样,我收到一个错误:匿名类型成员声明符无效。匿名类型成员必须使用成员赋值、简单名称或成员访问进行声明。
这是正确的方式还是足够的方式来说“从 a.ipid=i.id 上的内部连接 i 内部加入 i.uid=u.id 上的 u”?如果没有,请提供一份。谢谢。
var query = from a in db.tblActivities from i in db.tblIPs from u in db.tblUsers select new { u.UserName == null ? i.Address : u.UserName, a.Request, a.DateTime };
采纳答案by GalacticCowboy
When creating an anonymous type (what you're doing with the "new" without specifying a type) you have to specify the member name for each property. From your example, it would look something like this: (also fixed your joins)
创建匿名类型时(您在不指定类型的情况下使用“new”进行的操作)您必须为每个属性指定成员名称。从您的示例中,它看起来像这样:(也修复了您的连接)
var query = from a in db.tblActivities
join i in db.tblIPs on a.ipid equals i.id
join u in db.tblUsers on i.uid equals u.id
select new {
UserName = (u.UserName ?? i.Address),
Request = a.Request,
Date = a.DateTime
};
You could probably do the UserName your way, too:
您也可以按照自己的方式使用 UserName:
UserName = (u.UserName == null) ? i.Address : u.UserName,
but the ?? operator is more concise. It's similar to "isnull" in SQL.
但是??运算符更简洁。它类似于 SQL 中的“isnull”。
回答by LeppyR64
I'm fairly new to Linq to SQL but I'm pretty sure it would go like this:
我对 Linq to SQL 还很陌生,但我很确定它会是这样的:
var query =
from a in db.tblActivities
from i in a.tblIPs
from u in i.tblUsers
select new
{
userName = (u.UserName == null)
? i.Address
: u.UserName,
a.Request,
a.DateTime
};
The if statement needs to be in parentheses and the results outside of them. As for the joins, you follow the chain down from one->many.
if 语句需要在括号中,结果在括号之外。至于连接,您可以从一个-> 多个链下来。
回答by CMS
You have to use the join keyword, and define the relationship between the entities in order to make a proper inner join.
您必须使用 join 关键字,并定义实体之间的关系以进行适当的内部连接。
Hereyou can find some examples about that, I also highly recommend you to get LinqPad, its a really valuable tool for testing your queries, also its very good to learn, it has 200+ examples.
在这里你可以找到一些关于这方面的例子,我也强烈建议你得到LinqPad,它是一个非常有价值的测试查询的工具,也很好学习,它有 200 多个例子。
回答by Hugoware
if you're checking just for null, you can also use ??
如果您只是检查空值,您还可以使用 ??
string something = null;
string somethingElse = something ?? "default value";
As for the examples above, it is correct to do the ones that go...
至于上面的例子,做那些去的那些是正确的......
string something = (somethingElse == null ? "If it is true" : "if it is false");
The parens aren't required, but they do aid in reading.
括号不是必需的,但它们确实有助于阅读。
回答by user36784
Really. this question depends on the particular implementation of IQueryable that your linq expression will return. I see that you have db.XXX so are you using linq to sql or some linq to data store? If so, the specific implementation of IQueryable will need to have a way to translate your expression into a store expression. Other than the above comments, some of the other comments are correct that in an anonymous type you must specify a name for each member. This is really your error.
真的。这个问题取决于您的 linq 表达式将返回的 IQueryable 的特定实现。我看到你有 db.XXX 那么你是使用 linq to sql 还是一些 linq to data store?如果是这样,IQueryable 的具体实现将需要有一种方法将您的表达式转换为存储表达式。除了上面的注释,其他一些注释是正确的,在匿名类型中,您必须为每个成员指定一个名称。这真的是你的错误。