简体   繁体   English

在关联表中选择

[英]Select across associative tables

Given the data model below I would like to select every ConfigurableItem that has a Schedule where IsActive =true. 给定下面的数据模型,我想选择每个具有Schedule的ConfigurableItem,其中IsActive = true。

在此处输入图片说明

I have looked at numerous examples re: associative tables and don't really get any of them due to the examples somehow magically ignoring the many-to-many association. 我看了无数有关re:关联表的示例,但由于这些示例以某种方式神奇地忽略了多对多关联,因此并没有真正得到它们。 There seems to be a lot of suggestion that I should be able to : 似乎有很多建议我应该能够:

var f = from citem in context.ConfigurableItems
        where citem.ConfigurableItemSchedules.Schedule.IsActive == true
        select citem;

But that doesn't intellisense / compile. 但是,这不是智能/编译。 What am I missing here? 我在这里想念什么?

UPDATE: 更新:

Im using a .dbml autogenerated from drag and drop from server explorer (sql server) so below is some code that is auto generated that may help answer some of the comments. 我使用从服务器资源管理器(sql server)中拖放生成的.dbml,所以下面是一些自动生成的代码,这些代码可能有助于回答某些注释。 They're just truncated snippets of the generated fields. 它们只是生成字段的截断片段。

public partial class ConfigurableItem : INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private long _ConfigurableItemIndexCode;

        private string _ItemRootPath;

        private string _ItemName;

        private string _HandlerAssembly;

        private string _HandlerType;

        private EntitySet<ConfigurableItemProperty> _ConfigurableItemProperties;

        private EntitySet<ConfigurableItemSchedule> _ConfigurableItemSchedules;

.... ....

public partial class ConfigurableItemSchedule : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private long _ConfigurableItemIndexCode;

    private long _ScheduleIndexCode;

    private EntityRef<ConfigurableItem> _ConfigurableItem;

    private EntityRef<Schedule> _Schedule; 

I think you need: 我认为您需要:

var f = from citem in context.ConfigurableItems
        where citem.ConfigurableItemSchedules.Any(s=>s.Schedule.IsActive)
        select citem;

or something like that. 或类似的东西。

You are trying to reach Schedule but as ConfigurableItemSchedules itself has many rows (probably , I think) , compiler can not understand you need the schedules of which row.When you are using navigation properties in join you should be sure that the navigation property ends to just one row in destination Table or you should use .SelectMany(t=>...) or .any() to show compiler that you will select a collection of rows , may be it's better to start from the most bottom table like 您正在尝试到达Schedule,但是由于ConfigurableItemSchedules本身有很多行(可能是我认为),编译器无法理解您需要哪一行的时间表。当在联接中使用导航属性时,应确保导航属性以目标表中只有一行,或者您应该使用.SelectMany(t=>...).any()来向编译器显示您将选择一组行,也许最好从最底层的表开始,例如

var c = (from u in  context.Schedule
                     where u.IsActive == true
                     select u.ConfigurableItemSchedules.ConfigurableItems);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM