简体   繁体   English

Linq不支持SQL转换

[英]Linq no supported translation to SQL

I'm getting the error: 我收到错误消息:

Method 'System.Decimal getMeanRatingForGame(Int32)' has no supported translation to SQL. 方法'System.Decimal getMeanRatingForGame(Int32)'不支持SQL转换。

For the linq Line: 对于linq线:

let R = getMeanRatingForGame(c.ID)

Here's the linq in full: 这是完整的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++;
            }
        }

And here's the function throwing the error: 这是引发错误的函数:

/// <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;
}

Am I not allowed to use a function in a linq statement like this? 我不能在这样的linq语句中使用函数吗?

You are hitting the point where the abstraction breaks down. 您正在达到抽象崩溃的地步。

A LINQ provider translates from the LINQ to something the underlying data store can understand (in this case SQL). LINQ提供程序将LINQ转换为基础数据存储可以理解的内容(在这种情况下为SQL)。

As you can imagine, SQL does not have a getMeanRatingForGame function, so LINQ to SQL fails (as would EF, LINQ to Oracle etc...). 可以想象,SQL没有getMeanRatingForGame函数,因此LINQ to SQL失败(EF,LINQ to Oracle等也将失败)。

One solution is to break your query into two (or more) parts - one that is just interacting with SQL and that SQL will not have a problem with. 一种解决方案是将查询分为两个(或多个)部分-一个部分仅与SQL交互,而SQL不会出现问题。

Use the result with another query that does use getMeanRatingForGame - getting LINQ to Objects to do its magic. 将结果与另一个确实使用getMeanRatingForGame查询一起使用-使LINQ to Objects发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM