我正在使用linq到sql。我正在運行一個查詢,它使用一個函數來返回正確的記錄。我正在使用一個函數,所以我可以使用if語句,我不知道如何在linq子句中做到這一點。代碼複製如下。它返回一個錯誤:「GetPageOwner(int 32)沒有支持轉換爲sql」我做錯了什麼?如何解決它以獲得相同的結果?沒有支持翻譯爲SQL錯誤?
return (from page select new Result
{
pageOwner = GetPageOwner(page.page_id)
});
public Post GetPageOwner(int pageid)
{
var posts = (from dp in db.Posts where dp.pageid == pageid select dp);
var returned = posts;
if (posts.Count() > 0)
{
var latest = posts.OrderByDescending(o => o.Date).FirstOrDefault();
var sharedsamedayaslatest = (from p in posts where p.Date.AddDays(1) >= latest.Date select p);
if (sharedsamedayaslatest.Count() > 1)
{
var followedpost = (from p in posts from s in db.Subscriptions where s.Subscriber == UID && s.Subscribedto == p.UserId select p);
var count = followedpost.Count();
if (count == 1)
{
returned = followedpost;
}
else if (count > 1)
{
returned = (from s in followedpost let reposts = GetPostReposts(s.id) let rating = GetPostRating(s.id) let score = reposts + rating orderby score descending select s);
}
else
{
//no follower shared this post so return the most liked
returned = (from s in sharedsamedayaslatest let reposts = GetPostReposts(s.id) let rating = GetPostRating(s.id) let score = reposts + rating orderby score descending select s);
}
}
else
{
//no shares on the day the latest share
returned = sharedsamedayaslatest;
}
}
else
{
//only one post
returned = posts;
}
return returned.FirstOrDefault(); //order by userid gets a random one
}
http://stackoverflow.com/questions/332670/simple-linq-to-sql-has-no-supported-translation-to-sql – 2012-02-20 07:48:16
投票關閉 - 確切的雙重答案的另一個問題。 Pavel在第一條評論中提供了鏈接。 – TomTom 2012-02-20 07:52:14
在退貨部分之前還有其他的東西嗎? – Bastardo 2012-02-20 08:08:01