如何将多个 linq 语句转换为 selectmany

发布于 2024-08-07 21:58:22 字数 477 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苹果你个爱泡泡 2024-08-14 21:58:22

如果您想要单个查询,则可以将选择值设置为匿名类型。但返回值的范围是创建类型的方法。

var result = (from a in StoredProcedureParameters.Tables[0].AsEnumerable()
                 where a.Field<string>("ViewName") == displayName
                 select new { View = a.Field<string>("View"),
                              Date = d.Field<DateTime>("Date") }).Single();

那么变量 result 将是一个具有“View”和“Date”属性的对象。但您无法从方法返回该值。如果需要返回值,可以创建一个简单的类,

public class ViewDate
{
  public string View { get; set; }
  public DateTime Date { get; set; }
}

然后使用对象初始值设定项,最终将得到包含 ViewDate 对象实例的结果,您可以从方法中返回该实例。

var result = (from a in StoredProcedureParameters.Tables[0].AsEnumerable()
                 where a.Field<string>("ViewName") == displayName
                 select new ViewDate() { View = a.Field<string>("View"),
                              Date = d.Field<DateTime>("Date") }).Single();

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.

var result = (from a in StoredProcedureParameters.Tables[0].AsEnumerable()
                 where a.Field<string>("ViewName") == displayName
                 select new { View = a.Field<string>("View"),
                              Date = d.Field<DateTime>("Date") }).Single();

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

public class ViewDate
{
  public string View { get; set; }
  public DateTime Date { get; set; }
}

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.

var result = (from a in StoredProcedureParameters.Tables[0].AsEnumerable()
                 where a.Field<string>("ViewName") == displayName
                 select new ViewDate() { View = a.Field<string>("View"),
                              Date = d.Field<DateTime>("Date") }).Single();
2024-08-14 21:58:22

我不认为 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文