如何将多个 linq 语句转换为 selectmany
我是 LINQ 和 lambda 函数的初学者。我想知道如何使用 selectmany 函数将多个 linq 语句转换为一个语句。
string viewname = (from a in StoredProcedureParameters.Tables[0].AsEnumerable()
where a.Field<string>("ViewName") == displayName
select a.Field<string>("View")).Single();
DateTime date = (from d in StoredProcedureParameters.Tables[0].AsEnumerable()
select d.Field<DateTime>("Date")).First();
提前感谢您的帮助。
I am a beginner with LINQ and lambda function's. I was wondering how can i convert multiple linq statement to one statement using selectmany function.
string viewname = (from a in StoredProcedureParameters.Tables[0].AsEnumerable()
where a.Field<string>("ViewName") == displayName
select a.Field<string>("View")).Single();
DateTime date = (from d in StoredProcedureParameters.Tables[0].AsEnumerable()
select d.Field<DateTime>("Date")).First();
Thanks for help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要单个查询,则可以将选择值设置为匿名类型。但返回值的范围是创建类型的方法。
那么变量 result 将是一个具有“View”和“Date”属性的对象。但您无法从方法返回该值。如果需要返回值,可以创建一个简单的类,
然后使用对象初始值设定项,最终将得到包含 ViewDate 对象实例的结果,您可以从方法中返回该实例。
If you want a single query, you could have the select value be an anonymous type. But the scope of the return value is the method where the type is created.
then the variable result will be an object with the properties 'View' and 'Date'. You can't return this value from a method though. If you need to return the value, you could create a simple class
Then using an object initializer, you will end up with result containing an instance of the ViewDate object, which you can return from your method.
我不认为
SelectMany
会做你认为它做的事情。这通常只是一个展平操作。为什么您认为在这种情况下它会对您有帮助?为什么要避免两次查询?I don't think
SelectMany
does what you think it does. It's usually just a flatten operation. Why do you think it would help you in this case? Why do you want to avoid two queries?