日期:2014-05-20  浏览次数:20957 次

Linq 查询排序的问题..有点复杂!欢迎指教..
IEnumerable<DataAccess.t_photo_gallery> gallery = 

  (from g in g_DataContext.t_photo_gallery

  where (g.CompanyId == companyID)

  && ((g.PhotoPath == null) ? (false) : (g.PhotoPath.Contains(searchKeyWord))

  || ((g.Keyword == null) ? (false) : (g.Keyword.Contains(searchKeyWord)))

  || ((g.Description == null) ? (false) : g.Description.Contains(searchKeyWord)))

  && (((g.IptcInfo == null) ? (false) : (g.IptcInfo.Contains(itpcExifKeyword)))

  || ((g.ExifInfo == null) ? (false) : g.ExifInfo.Contains(itpcExifKeyword)))

  orderby 

  searchKeyWord != "" ? utility.CalculateStringScore(searchKeyWord,g.Keyword) : 0 这一行报错

  ,g.ModifiedTime descending

  select g).Skip(pageSize * pageIndex).Take(pageSize);

  return gallery.ToList();

-----------------------------------------------
报错的那一行是想根据返回的int还排序;

错误信息: Method 'Int32 CalculateStringScore(System.String, System.String)' has no supported translation to 
SQL.
希望能得到各位高手的帮助;
根据查询的结果,排序...但不是简单的排序...

------解决方案--------------------
单步调试 试试
------解决方案--------------------
探讨
引用:
utility.CalculateStringScore(searchKeyWord,g.Keyword)是C#的Method吧?
这个方程式必须是SQL Stored Procedure或者Function的mapper而不是独立的C#逻辑。

LINQ to SQL supports stored procedures and user-defined functions. In LINQ to SQL, you map these database-defined abstractions to client objects so that you can access them in a strongly typed manner from client code. The method signatures resemble as closely as possible the signatures of the procedures and functions defined in the database. You can use IntelliSense to discover these methods.

全文:http://msdn.microsoft.com/en-us/bb386989.aspx

你看看这篇文章,主要是最后一段"LINQ to SQL Methods and Database Stored Procedures "。
你需要把CalculateStringScore的逻辑翻译成Stored Proc,再通过C# Method map过去。

如果还有问题的话,多贴点代码上来,尤其是utility.CalculateStringScore的代码。


不太看得懂..我现在把整个方法的代码贴了出来..方法我基本上改了。还出现在如上的错误..

------解决方案--------------------
感觉类型的问题
C# code

searchKeyWord != "" ? utility.CalculateStringScore(searchKeyWord,g.Keyword) : 0  
//searchKeyWord 这个什么类型啊?就算是匿名类型也应该是强类型的吧?Int? String?