本文介绍了通过使用C#在LINQ中使用外键导航属性来查询表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 LINQ to SQL EF 类:

具有在 SessionId (主表 WebinarSession )上的外键关系.

with the foreign key relationship on SessionId (primary table WebinarSession).

我想使用lambdas 来选择与特定产品系列有关的所有 WebinSession 行.

I would like, by using lambdas to select all the WebinarSession rows that concern a specific product line.

我以该代码为例,该代码当然不起作用(它应该与其中一起使用 Single 设置,但不适用)因为我有多个符合条件的实例):

I put as example this code that of course DOES NOT WORK(it would owrk with Single inseatd of Where but it is not applicable because I have mulitple instances matching the condition):

SessionId = _webinarRecordingsDB.WebinarSessions.Where(m => m.SessionId == m.SessionSubjects.Where(n => n.ProductLineName == productLine).SessionId);

_webinarRecordingsDB 是映射 SQL数据库的EF对象.

有人知道如何完成这项任务吗?谢谢

Does anybody know how to fulfill this task? Thanks

推荐答案

这应该可以解决问题:

var sessions = _webinarRecordingsDB.WebinarSessions.Where(w => w.SessionSubjects.Any(s => s.ProductLine == productLine));

在EF(或Linq to SQL类)中定义外键时,设计人员应自动创建一个属性,该属性将允许您从实体直接访问表(在这种情况下,您的WebinarSession对象应具有一个称为SessionSubjects的属性).

When you define a foreign key in EF (or Linq to SQL classes) the designer should automatically create a property which would allow you direct access to the table from your entity (in your case your WebinarSession object should have a property called SessionSubjects).

这篇关于通过使用C#在LINQ中使用外键导航属性来查询表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:27