是什么原因导致的Linq错误:此方法不能被翻译成店前pression? [英] What causes the Linq error: This method cannot be translated into a store expression?

查看:140
本文介绍了是什么原因导致的Linq错误:此方法不能被翻译成店前pression?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆的LINQ到实体的方法有相同的SELECT语句,所以我想我会很聪明,单独说出来到它自己的方法来减少冗余......但是当我试图运行code,我得到了下面的错误...

  

这种方法不能被翻译成   商店前pression

下面是我创建的方法...

 公共用户的getUser(DBUSER的用户,长UID)
{
    返回新用户
    {
        UID = user.uid,
        名字= user.first_name,
        姓氏= user.last_name
    };
}
 

和我打电话在这样的方法......

 公共用户的getUser(长UID)
{
    使用(VAR实体=新myEntities()){
        返回
            entities.DbUsers.Where(X => x.uid == UID和放大器;&安培; x.account_status ==(短)AccountStatus.Active)。
                选择(X =>的getUser(X,UID)).FirstOrDefault();
    }
}
 

更新:这里是code,工程内联

 公共用户的getUser(长UID,长uid_user)
        {
            使用(VAR实体=新myEntities())
            {

                变种Q =从u在​​entities.DbUsers
                        其中,u.uid == uid_user
                        选择新用户
                        {
                            UID = u.uid,
                            名字= u.first_name,
                            姓氏= u.last_name,
                            BigPicUrl = u.pic_big,
                            生日= u.birthday,
                            SmallPicUrl = u.pic_small,
                            SquarePicUrl = u.pic_square,
                            区域设置= u.locale.Trim()
                            IsFavorite = u.FavoriteFriends1.Any(X => x.uid ==流体),
                            FavoriteFriendCount = u.FavoriteFriends.Count,
                            LastWishlistUpdate = u.WishListItems.OrderByDescending(X => x.added)。.FirstOrDefault()补充说:
                            性别=(UserSex)u.sex
                        };

                变种用户= q.FirstOrDefault();
                user.DaysUntilBirthday = user.Birthday.DaysUntilBirthday();
                返回用户;
            }
        }
 

解决方案

该错误是发现的,你不能它转换成一个T-SQL(或P-SQL)查询。

您需要确保您尝试水合物成一些其他类型之前,你已经执行查询。

保持简单,使用扩展方法。这是他们来这里的目的。

 公共静态用户ToUserEntity(此DBUSER用户)
{
    返回新用户
    {
        UID = user.uid,
        名字= user.first_name,
        姓氏= user.last_name
    };
}
 

然后在您的DAL:

 公共用户的getUser(长UID)
{
    用户DBUSER;

    使用(VAR实体=新myEntities())
    {
        DBUSER = entities.DbUsers
                  。凡(X => x.uid == UID和放大器;&安培; x.account_status ==(短)AccountStatus.Active)
                 .FirstOrDefault(); //查询执行对数据库
    }

    返回dbUser.ToUserEntity();
}
 

看我怎么滋润POCO到对象的的情况下已被释放?通过这种方式,可以确保英孚已经完成了它的前pression工作,试图来滋润到自定义对象之前。

此外,我不知道为什么你传递uid来的方法,它甚至没有被使用。

在进一步的笔记,你不应该的需要的做这种事情(项目EF POCO的到你自己的对象)。

如果你这样做,这是一个很好的例子定制POCO的(表映射直接进入您的自定义POCO的,不要使用code代)。

I have a bunch of Linq to Entity methods that had the same select statement, so I thought I would be clever and separate that out into it's own method to reduce redundancy... but when i attempted to run the code, i got the following error...

this method cannot be translated into a store expression

Here is the method i created...

public User GetUser(DbUser user, long uid)
{
    return new User
    {
        Uid = user.uid,
        FirstName = user.first_name,
        LastName = user.last_name
    };
}

And am calling in a method like this...

public User GetUser(long uid)
{
    using (var entities = new myEntities()) {
        return
            entities.DbUsers.Where( x => x.uid == uid && x.account_status == ( short )AccountStatus.Active ).
                Select( x => GetUser( x, uid ) ).FirstOrDefault( );
    }
}

UPDATE: here is the code that works inline

public User GetUser(long uid, long uid_user)
        {
            using (var entities = new myEntities())
            {

                var q = from u in entities.DbUsers
                        where u.uid == uid_user
                        select new User
                        {
                            Uid = u.uid,
                            FirstName = u.first_name,
                            LastName = u.last_name,
                            BigPicUrl = u.pic_big,
                            Birthday = u.birthday,
                            SmallPicUrl = u.pic_small,
                            SquarePicUrl = u.pic_square,
                            Locale = u.locale.Trim(),
                            IsFavorite = u.FavoriteFriends1.Any(x => x.uid == uid),
                            FavoriteFriendCount = u.FavoriteFriends.Count,
                            LastWishlistUpdate = u.WishListItems.OrderByDescending(x => x.added).FirstOrDefault().added,
                            Sex = (UserSex)u.sex
                        };

                var user = q.FirstOrDefault();
                user.DaysUntilBirthday = user.Birthday.DaysUntilBirthday();
                return user;
            }
        }

解决方案

The error is spot on, you can't translate that into a T-SQL (or P-SQL) query.

You need to make sure you've executed the query before you attempt to hydrate it into some other type.

Keep it simple, use an extension method. That's what they are there for.

public static User ToUserEntity(this DbUser user)
{
    return new User
    {
        Uid = user.uid,
        FirstName = user.first_name,
        LastName = user.last_name
    };
}

Then in your DAL:

public User GetUser(long uid)
{
    User dbUser;

    using (var entities = new myEntities())
    {
        dbUser = entities.DbUsers
                  .Where( x => x.uid == uid && x.account_status == (short)AccountStatus.Active )
                 .FirstOrDefault(); // query executed against DB
    }

    return dbUser.ToUserEntity();
}

See how i hydrate the POCO into an object after the context has been disposed? This way, you ensure EF has finished it's expression work before you attempt to hydrate into a custom object.

Also i dont know why you're passing uid to that method, it's not even being used.

On a further note, you shouldn't need to do this kind of thing (project EF POCO's into your own objects).

If you do, it's a good case for custom POCO's (map the tables straight into your custom POCO's, don't use the Code Generation).

这篇关于是什么原因导致的Linq错误:此方法不能被翻译成店前pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆