Reputation: 6546
The error below occur when using ToObservableCollection() Any ideas why ?
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The entity or complex type 'xxxx_DataModel.paytypes' cannot be constructed in a LINQ to Entities query.
var payTypes = (from payTypeTbl in Db.paytypes
where payTypeTbl.bActive == true
select new paytypes
{
iPayTypeId = payTypeTbl.iPayTypeId,
sImg = @"img\" + payTypeTbl.sImg,
sPayTypeName = payTypeTbl.sPayTypeName,
}
).ToObservableCollection();
public static class Extensions
{
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> col)
{
return new ObservableCollection<T>(col);
}
}
Upvotes: 1
Views: 113
Reputation: 6546
Ok, the solution is this. Is there a shorter way than this ? Or everytime I have to go through this pain of writing the repetition ?
PayType = (from payTypeTbl in Db.paytypes
where payTypeTbl.bActive == true
select new
{
iPayTypeId = payTypeTbl.iPayTypeId,
sImg = payTypeTbl.sImg,
sPayTypeName = payTypeTbl.sPayTypeName,
}).ToList().Select(x => new paytypes {
iPayTypeId = x.iPayTypeId,
sImg = @"img\" + x.sImg,
sPayTypeName = x.sPayTypeName,
} ).ToObservableCollection();
Upvotes: 0
Reputation: 3889
From your code it looks as though one of your data types on the database has been defined as Image. As far as I am aware EF does not support this data type and this will be why you are receiving this error.
Upvotes: 0