2011-01-06 64 views
0

我想从表“Province_Notifiers”提取数据,并从表“Province_Notifier_Datas”中提取所有相应的项目。表“Province_Notifier”有一个GUID来标识它(PK),表“Province_Notifier_Datas”有一个名为BelongsToProvinceID的列,女巫是“Province_Notifier”表GUID的外键。从表1中选择行并将表2中的所有子元素选择到对象中

我想是这样的:

var records = from data in ctx.Province_Notifiers 
       where DateTime.Now >= data.SendTime && data.Sent == false 
       join data2 in ctx.Province_Notifier_Datas on data.Province_ID equals data2.BelongsToProvince_ID 
       select new Province_Notifier 
       { 
        Email = data.Email, 
        Province_ID = data.Province_ID, 
        ProvinceName = data.ProvinceName, 
        Sent = data.Sent, 
        UserName = data.UserName, 
        User_ID = data.User_ID, 
        Province_Notifier_Datas = (new List<Province_Notifier_Data>().AddRange(data2)) 
       }; 

这条线不能正常工作,我试图找出如何topull从表2中的数据成Province_Notifier_Datas变量。

Province_Notifier_Datas = (new List<Province_Notifier_Data>().AddRange(data2)) 

我可以通过添加第二个表行插入Province_Notifier_Datas轻松添加一条记录,但我不能把它抓回来。

Province_Notifier dbNotifier = new Province_Notifier(); 
// set some values for dbNotifier 
dbNotifier.Province_Notifier_Datas.Add(
      new Province_Notifier_Data 
      { 
       BelongsToProvince_ID = userInput.Value.ProvinceId, 
       EventText = GenerateNotificationDetail(notifierDetail) 
      }); 

这可以正常工作并将数据插入到两个表中。

编辑:

这些错误信息被抛出:

Cannot convert from 'Province_Notifier_Data' to 'System.Collections.Generic.IEnumerable'

如果我在Visual Studio中的样子,变量 “Province_Notifier_Datas” 的类型是System.Data.Linq.EntitySet的

The best overloaded method match for 'System.Collections.Generic.List.AddRange(System.Collections.Generic.IEnumerable)' has some invalid arguments

编辑

var records = from data in ctx.Province_Notifiers 
       where DateTime.Now >= data.SendTime && data.Sent == false   
       join data2 in ctx.Province_Notifier_Datas on data.Province_ID equals data2.BelongsToProvince_ID 
       into data2list 
       select new Province_Notifier 
       { 
        Email = data.Email, 
        Province_ID = data.Province_ID, 
        ProvinceName = data.ProvinceName, 
        Sent = data.Sent, 
        UserName = data.UserName, 
        User_ID = data.User_ID, 
        Province_Notifier_Datas = new EntitySet<Province_Notifier_Data>().AddRange(data2List) 
       }; 

错误3名称'data2List'在当前上下文中不存在。

回答

0

data2代表Province_Notifier_Data类型的单个对象,而不是枚举(因此是例外)。尝试插入一个into条款到你的查询:

var records = from data in ctx.Province_Notifiers 
       where DateTime.Now >= data.SendTime && data.Sent == false 
       join data2 in ctx.Province_Notifier_Datas 
        on data.Province_ID equals data2.BelongsToProvince_ID 
       into data2List 
       select new Province_Notifier 
       { 
        Email = data.Email, 
        Province_ID = data.Province_ID, 
        ProvinceName = data.ProvinceName, 
        Sent = data.Sent, 
        UserName = data.UserName, 
        User_ID = data.User_ID, 
        Province_Notifier_Datas = data2List 
       }; 

如果Province_Notifier_Datas的类型为EntitySet<T>,你可以尝试以下方法:(从here想法)定义的扩展方法:

public static class Extensions 
{ 
    public static EntitySet<T> ToEntitySet<T>(this IEnumerable<T> source) 
     where T : class 
    { 
     var entitySet = new EntitySet<T>(); 
     entitySet.AddRange(source); 
     return entitySet; 
    } 
} 

,然后替换最后一行:

Province_Notifier_Datas = data2List.ToEntitySet() 
+0

当我尝试我得到以下错误: 错误无法将类型'System.Collections.Generic.IEnumerable '隐式转换为'System.Data.Linq.EntitySet '。存在明确的转换(您是否缺少演员?) – Patrick 2011-01-06 19:31:12