William
William

Reputation: 6546

The entity or complex type 'xxxx_DataModel.paytypes' cannot be constructed in a LINQ to Entities query

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

Answers (2)

William
William

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

bilpor
bilpor

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

Related Questions