簡體   English   中英

Linq不支持SQL轉換

[英]Linq no supported translation to SQL

我收到錯誤消息:

方法'System.Decimal getMeanRatingForGame(Int32)'不支持SQL轉換。

對於linq線:

let R = getMeanRatingForGame(c.ID)

這是完整的linq:

private static Game[] getTopRatedGamesDo(int Fetch, int CategoryID, int Skip)
{
    /**
        weighted rating (WR) = ((v ÷ (v+m)) × R + (m ÷ (v+m)) × C) x E
        where:
        R = average for the game
        v = number of votes for the game
        m = minimum votes required to be listed
        C = the mean weighted vote across the whole report
        E = Is not example game (example game = 0, not example = 1)
    */
    Game[] r;
    using (MainContext db = new MainContext())
    {
        // Mean
        decimal C = getMeanRatingForCat(CategoryID);

        // Min votes to be considered
        decimal m = Settings.GameVotesReqdForTopRatedBoards;

        // Entire games list
        if (CategoryID == 0)
        {
            var q = (from c in db.tblArcadeGames
                        let v = (decimal)db.tblArcadeGameVotes.Where(v => v.GameID == c.ID).Count()
                        let R = getMeanRatingForGame(c.ID)
                        let E = (c.CategoryID == 2 ? (decimal)0.1 : (decimal)1)
                        let WR = (((v / (v + m)) * R + (m / (v + m)) * C) * E)
                        where c.IsDeleted == false
                        && c.Approved == true
                        && c.TotalVotes >= Settings.GameVotesReqdForTopRatedBoards
                        orderby WR descending
                        select new { c, WR})
                .Skip(Skip)
                .Take(Fetch);

            r = new Game[q.Count()];
            int i = 0;
            foreach (var g in q)
            {
                r[i] = new Game(g.c, g.WR);
                i++;
            }
        }

這是引發錯誤的函數:

/// <summary>
/// Gets the mean rating of a game.
/// </summary>
public static decimal getMeanRatingForGame(int GameID)
{
    /**
    Try multiplying each voter's rating base 10 logarithm of the rep of the voter,
        * then finding the weighted rating, then dividing by the mean of the base 10 logarithms
        * of the reps of the voters. Change the base from 10 to something like 2 if you want heavier weighting.
        * */
    decimal C = 0;
    using (MainContext db = new MainContext())
    {
        var q = (from c in db.tblArcadeGameVotes
                    where c.GameID == GameID
                let UserWeighting = (decimal)Math.Log((double)db.tblProfiles.Where(u => u.UserID == c.UserID).Single().Reputation, 10)
                let WeightedVote = UserWeighting * (decimal)c.Score
                select new { UserWeighting, WeightedVote });

        decimal AverageUserWeighting = (decimal)q.Sum(c => c.UserWeighting) / (decimal)q.Count();
        decimal AverageWeightedVote = (decimal)q.Sum(c => c.WeightedVote) / (decimal)q.Count();

        C = AverageWeightedVote / AverageUserWeighting;
    }
    return C;
}

我不能在這樣的linq語句中使用函數嗎?

您正在達到抽象崩潰的地步。

LINQ提供程序將LINQ轉換為基礎數據存儲可以理解的內容(在這種情況下為SQL)。

可以想象,SQL沒有getMeanRatingForGame函數,因此LINQ to SQL失敗(EF,LINQ to Oracle等也將失敗)。

一種解決方案是將查詢分為兩個(或多個)部分-一個部分僅與SQL交互,而SQL不會出現問題。

將結果與另一個確實使用getMeanRatingForGame查詢一起使用-使LINQ to Objects發揮作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM