我想從表「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'在當前上下文中不存在。
當我嘗試我得到以下錯誤: 錯誤無法將類型'System.Collections.Generic.IEnumerable'隱式轉換爲'System.Data.Linq.EntitySet '。存在明確的轉換(您是否缺少演員?) –
Patrick
2011-01-06 19:31:12