x.ToString()不支持的实体框架! [英] x.ToString() is not supported by the entity framework!

查看:292
本文介绍了x.ToString()不支持的实体框架!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架不承认我的ToString方法,该路由对象转换为specifix字符串:

Entity framework doesn't recognize my ToString method that converts the route object to specifix string:

public IEnumerable<Route> GetRoutes(string prefix, string searchPattern, string code)
{
    return Routes.
        Where(x => code == null || x.Code == code).
        Where(x => searchPattern == null || x.ToString().Contains(searchPattern)).
        Where(x => prefix == null || x.ToString().StartsWith(prefix));
}

下面是我的路线实体:

 public class Route : IDataEntity, ISoftDeletable
    {
        public virtual long Id { get; set; }
        public virtual string Code { get; set; }
        public virtual bool IsDeleted { get; set; }
        public virtual Guid CompanyId { get; set; }
        public virtual IList<LocationInRoute> Locations { get; set; }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();
            foreach (LocationInRoute loc in Locations)
            {
                if (str.Length > 0)
                {
                    str.Append(" > ");
                }
                str.Append(loc.ToString());
            }
            return str.ToString();
        }
    }

所有 x.ToString()抛出一个异常,不支持在LINQ to实体.. 任何解决办法?

All the x.ToString() throws an exception of not supported in linq to entities.. Any workaround?

推荐答案

您不能在客户端(的ToString )。

您必须创建SQL函数,它会做你的评价 - 它可以做你的位置只有字符串连接(我认为这是相关的实体),或者你可以在自定义函数整个测试。然后,你可以导入功能到你的实体模型(EDMX),并创建 EdmFunctionAttribute 来称呼它 - 的 MSDN

You must create SQL function which will do your evaluation - it can either do only string concatenation of you locations (I believe it is related entity) or you can do the whole test in custom function. Then you can import that function into your entity model (EDMX) and create EdmFunctionAttribute to call it - MSDN.

这篇关于x.ToString()不支持的实体框架!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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