为 LINQ 查询设置 SQL 关系

发布于 2024-09-01 02:40:47 字数 379 浏览 8 评论 0原文

我有两个表,我试图在它们之间创建关系,这样我就可以编写不需要联接的漂亮 LINQ 查询。

Widgets
WidgetId
WidgetDescription

Orders
OrderId
WidgetId
OrderDate

我想要做的是创建一个 LINQ 查询,它执行类似于以下操作的操作:

var result = from x in db.Widgets
Where x.Orders.OrderDate == "5/11/2010"
select x;

尽管在 SQL Server 中创建了关系,但我似乎无法让 Intellitext 获取其他数据库。我需要采取任何其他步骤才能完成这项工作吗?

I have two tables that I'm trying to create a relationship between so I can write nice LINQ queries that don't require join.

Widgets
WidgetId
WidgetDescription

Orders
OrderId
WidgetId
OrderDate

What I want to be able to do is create a LINQ query that does something similar to:

var result = from x in db.Widgets
Where x.Orders.OrderDate == "5/11/2010"
select x;

I can't seem to get intellitext to pick up the other database despite creating a relationship in SQL server. Are there any additional steps I need to take to make this work?

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

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

发布评论

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

评论(2

煮酒 2024-09-08 02:40:48

我认为如果不将 OrderDate 添加到数据库中的小部件,或者在查询之前运行联接查询,就无法完成此操作。

我会做这样的事情。

var firstQuery = (for w in Widgets
                  join o in Orders
                  on w.WidgetId equals o.WidgetID
                  select new {Widgets = w, OrderDate = o.OrderDate}).Distinct();

var secondQuery = for record in firstQuery
                  where record.OrderDate == "5/11/2010"
                  select record.Widgets;

这样您就可以获得每个 Widget 记录的 OrderDate

I don't think it could be done without either adding OrderDate to Widgets in your database, or running a join query before querying it.

I would do something like this.

var firstQuery = (for w in Widgets
                  join o in Orders
                  on w.WidgetId equals o.WidgetID
                  select new {Widgets = w, OrderDate = o.OrderDate}).Distinct();

var secondQuery = for record in firstQuery
                  where record.OrderDate == "5/11/2010"
                  select record.Widgets;

This way you get an OrderDate for each Widget record.

羞稚 2024-09-08 02:40:48

怎么样:

var result = from widgetId in 
                (from order in orderContext.Orders
                    where order.OrderDate == new DateTime(2010, 5, 11)
                    select order.WidgetId)
            from widget in widgetContext.Widgets
            where widget.WidgetId = widgetId
            select widget;

How about something like:

var result = from widgetId in 
                (from order in orderContext.Orders
                    where order.OrderDate == new DateTime(2010, 5, 11)
                    select order.WidgetId)
            from widget in widgetContext.Widgets
            where widget.WidgetId = widgetId
            select widget;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文