简体   繁体   中英

EF 4: The specified type member '*' is not supported in LINQ to Entities

i have a mapped-class like this:

[Table("MyTable")]
class MyClass         
{   
        //properties; id, name, etc...

        private string _queuedToWHTime = string.Empty;
        [Column("QueuedToWHTime")]
        public string QueuedToWHTime
        {
            get { return _queuedToWHTime; }
            set { _queuedToWHTime = value; }
        }

        public DateTime? QueuedToWHTime_DateTime
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(_queuedToWHTime))
                {
                    return Convert.ToDateTime(_queuedToWHTime);
                }
                return null;
            }
        }
}

and a table (MyTable):

CREATE TABLE webnews_in
(
Id                INT NOT NULL auto_increment,
QueuedToWHTime    VARCHAR (50) NULL
...
PRIMARY KEY (Id)
);

when i trying to query like this:

var searchRslt=(from m in queryableNews
    orderby m.QueuedToWHTime_DateTime descending
    select m).ToList();

I got a NotSupportedException : The specified type member 'QueuedToWHTime_DateTime' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

You cannot query with EF on custom properties. The custom property cannot be translated in SQL.

You can do this to force the orderby to be done 'in-memory'.

var searchRslt = queryableNews
    .AsEnumerable()
    .OrderBy(m => m.QueuedToWHTime_DateTime)
    .ToList();

The issue is here:

var searchRslt=(from m in queryableNews
orderby m.QueuedToWHTime_DateTime descending
select m).ToList();

you are trying to use the property .QueuedToWHTime_DateTime , but that does not exist in the database. You need to use the names that are used in the database. In this case .QueuedToWHTime So:

var searchRslt=(from m in queryableNews
orderby m.QueuedToWHTime descending
select m).ToList();

If the database propery is not usable in this scenario, you will have to pull the entire list, convert it to an IEnumerable (any will do), then filter/order that IEnumerable by its property.

Something like this:

var result = queryableNews.ToList().OrderbyDescending(x=>x.QueuedToWHTime_DateTime).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM