Cannot implicitly convert type IEnumerable<A> to System.Data.Linq.EntitySet. An explicit conversion exists (are you missing a cast?)
這是我開發時發生的問題。
大概是這樣的。
var q = from storeEl in buyerEl.Elements("store")
select new Store
{
StoreId = storeEl.Element("id").Value,
TaxNo = buyerEl.Value,
StoreName = storeEl.Element("cname").Value,
Emails = from sub in storeEl.Elements("mails")
select new StoreEmail
{
StoreId = storeEl.Attribute("id").Value,
Email = sub.Attribute("email").Value
},
};
問題出現在 from sub in 那一段。Emails 是一個 EntitySet<Email>,而非 IEnumerable<Email>,於是發生了錯誤。goole 了一下,找到了一個看來不是很好的方法。
public static class LinqExtension
{
public static EntitySet ToEntitySet(this IEnumerable source) where T : class
{
var es = new EntitySet();
es.AddRange(source);
return es;
}
}
然後,將程式改為
var q = from storeEl in buyerEl.Elements("store")
select new Store
{
StoreId = storeEl.Element("id").Value,
TaxNo = buyerEl.Value,
StoreName = storeEl.Element("cname").Value,
Emails = (from sub in storeEl.Elements("mails")
select new StoreEmail
{
StoreId = storeEl.Attribute("id").Value,
Email = sub.Attribute("email").Value
}).ToEntitySet(),
};
沒有留言:
張貼留言