2013-02-26 59 views
0

鑑於下面的數據模型,我想選擇每個具有計劃的ConfigurableItem,其中IsActive = true。在關聯表中選擇

enter image description here

我已經看過很多例子再次:關聯表,並不能真正得到任何人因例子神奇地忽略了許多-to-many關聯。似乎有很多建議,我應該能夠:

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

但是,這不智能/編譯。我在這裏錯過了什麼?

UPDATE:

進出口使用從服務器資源管理器(SQL服務器)拖放一個自動生成的.dbml所以下面是一些代碼,是自動生成的,可以幫助回答一些評論。它們只是生成字段的截斷片段。

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; 
+0

心不是ConfigurableItemSchedules一採集? – 2013-02-26 08:10:16

+0

你應該使用SelectMany,因爲你有很多行 – 2013-02-26 08:11:59

+0

@Tomas它是一個公共EntitySet ConfigurableItemSchedules – rism 2013-02-26 08:14:02

回答

2

我想你需要:

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

或類似的東西。

+0

不幸的是沒有檢測到日程安排,所以我無法應用。任何(s => s.IsActive)。錯誤消息是根據我對問題評論中的tschmit007的回覆。 – rism 2013-02-26 08:35:47

+0

我在幾分鐘前修改了答案。 – Phil 2013-02-26 08:36:32

+0

Yesss !!好一個。非常感謝。這是非常好的便士放棄了更多的事情:你的答案。 – rism 2013-02-26 08:37:26

1

您試圖達到Schedule,但由於ConfigurableItemSchedules本身有很多行(可能我認爲),編譯器無法理解您需要哪一行的時間表。當您在連接中使用導航屬性時,應確保導航屬性的目的地表結束,只是一個行或你應該使用.SelectMany(t=>...).any()表明編譯器,你將選擇行的集合,可能最好是從最底層表開始喜歡

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