實體框架並沒有認識到,路由對象轉換爲字符串specifix我的ToString方法:x.ToString()不被實體框架支持!
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 entities .. 任何解決方法?
請定義'Route'類您需要將它與「searchPattern」或「prefix」進行比較的屬性 – 2011-06-02 10:05:43
可能有助於提供有關Route的更多信息。 – tofutim 2011-06-02 10:05:44
請記住,您在實體查詢中鍵入的C#代碼永遠不會執行!相反,表達式樹會被評估並轉換爲SQL語句。 ToString()如何轉換爲SQL? – MattDavey 2011-06-02 10:06:09