如何通过 subsonic 对象从连接表返回数据?

发布于 2024-11-27 14:45:46 字数 308 浏览 8 评论 0原文

我在 Subsonic 3 上使用 ActiveRecord,实际上我想这样做:

  select * from foo
   left outer join bar on bar.Id = foo.barId
where foo.someProperty = 2

我编写了一个存储过程来获取数据,但 Subsonic 仅创建了对象来保存 foo 和 bar 中的列。

将数据返回到单个对象中以便我可以绑定它的最佳方法是什么。理想情况下,我希望它位于列表中<>但如果不写自己的类,似乎没有亚音速提供的方法。

I'm using ActiveRecord on Subsonic 3 and I effectively want to do this:

  select * from foo
   left outer join bar on bar.Id = foo.barId
where foo.someProperty = 2

I've written a stored procedure to fetch the data but Subsonic has only created objects to hold the columns from foo and bar.

What's the best way of returning the data into a single object so I can just bind it. Ideally I want it to be in a list<> but without writing my own class, there doesn't seem to be a way provided by subsonic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

岁月染过的梦 2024-12-04 14:45:46

这里有几个选项...

您可以创建一个执行连接的数据库视图,并让 SubSonic 为您的视图生成数据类型,然后您的选择就像从任何其他表中进行选择一样。

或者,您可以使用 Linq 表达式来连接到匿名或动态类型(如果您使用的是 .net 4)例如:

public List<dynamic> LoadData(int id)
{
  var data = from f in db.Foo
             from b in db.Bar.Where(x => x.Id == f.BarId).DefaultIfEmpty()
             where f.SomeProperty == id
             select new
             {
               SomeProperty = f.Something,
               AnotherProperty = b.SomethingElse
             };
  return data.Cast<dynamic>().ToList();
}

当然,另一种选择是执行上面的 Linq 表达式,但定义您自己的类来保存返回数据,并选择进去。

public class MyData
{
  public string SomeProperty { get; set; }
  public string AnotherProperty { get; set; }
}

public List<MyData> LoadData(int id)
{
  var data = from f in db.Foo
             from b in db.Bar.Where(x => x.Id == f.BarId).DefaultIfEmpty()
             where f.SomeProperty == id
             select new MyData()
             {
               SomeProperty = f.Something,
               AnotherProperty = b.SomethingElse
             };
  return data.ToList();
}

You have a couple options here...

You could create a database view that does your join, and have SubSonic generate a data type for your view, then your select would be just like selecting from any other table.

Alternatively, you could use a Linq expression to do the join into an anonymous or dynamic type (if you are using .net 4) For example:

public List<dynamic> LoadData(int id)
{
  var data = from f in db.Foo
             from b in db.Bar.Where(x => x.Id == f.BarId).DefaultIfEmpty()
             where f.SomeProperty == id
             select new
             {
               SomeProperty = f.Something,
               AnotherProperty = b.SomethingElse
             };
  return data.Cast<dynamic>().ToList();
}

Of course another alternative is to do the Linq expression above, but define your own class to hold the returned data, and select into it.

public class MyData
{
  public string SomeProperty { get; set; }
  public string AnotherProperty { get; set; }
}

public List<MyData> LoadData(int id)
{
  var data = from f in db.Foo
             from b in db.Bar.Where(x => x.Id == f.BarId).DefaultIfEmpty()
             where f.SomeProperty == id
             select new MyData()
             {
               SomeProperty = f.Something,
               AnotherProperty = b.SomethingElse
             };
  return data.ToList();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文