【问题标题】:Method'' has no supported translation to SQL方法'' 没有支持的 SQL 转换
【发布时间】:2013-10-15 20:16:33
【问题描述】:

我想在 linq to sql 查询 where 子句中检查公共 int。我收到此错误:方法 'Int32 isInDept(System.String)' 没有支持的 SQL 转换。

模糊相关的类(来自一个名为 ad 的公共静态类):

    //get AD property
    public static string GetProperty(this Principal principal, String property) {
        DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
        if (directoryEntry.Properties.Contains(property))
            return directoryEntry.Properties[property].Value.ToString();
        else
            return String.Empty;
    }

    public static string GetDepartment(this Principal principal) {
        return principal.GetProperty("department");
    }

有问题的班级(来自不同的班级):

    public int isInDept(string department) {
        PrincipalContext domain = new PrincipalContext(ContextType.Domain);
        UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, GetUserId());

        if (department == userPrincipal.GetDepartment()) {
            return 3;
        }
        else { return 2; }
    }

    public intranetGS.viewArticle viewArticle(int id) {
        string user = GetUserId();

        var result = ( from a in n.articles
                       join s in n.sections on a.section equals s.section_id
                       join p in n.privacies on a.privacy equals p.privacy_id
                       let iid = isInDept(s.name)
                       where (a.active == true && a.article_id == id && a.privacy < iid) ||
                       (a.active == true && a.article_id == id && a.privacy == 3 && a.author == user)
                       select new intranetGS.viewArticle {
                           articleId = a.article_id,
                           title = a.title,
                           author = a.author,
                           html = a.html,
                           section = s.name,
                           privacy = p.name,
                           dateCreated = a.date_created,
                           dateModified = a.date_modified,
                           userCreated = a.user_created,
                           userModified = a.user_modified
                       }).First();

        var nv = (from v in n.navs
                            join s in n.sections on v.section equals s.section_id
                            let iid = isInDept(s.name)
                            where (v.active == true && s.name == result.section && v.privacy  < 3) ||
                            (v.active == true && s.name == result.section && v.privacy == iid && v.user_created == user)
                            select v.html);

        StringBuilder sb = new StringBuilder();

        foreach (var r in nv) {
            sb.Append(nv);
        }

        result.articleNav = sb.ToString();

        return result;
    }

我做错了什么?如果我不能这样做,建议怎么做?

【问题讨论】:

  • 那么您希望将该方法翻译成什么 SQL?
  • 我希望确定 isInDept()。最终问题是隐私级别可能是 3(如果用户不在给定部门)或 2(如果用户在给定部门)。我想我没有完全理解这个问题。
  • 写出您希望查询被翻译成的 SQL。就是这么简单。我看不出有任何可能的方式让该代码发生这种情况。显然,linq to sql 查询提供程序也不能。
  • 作为替代方案,我想我可以在 viewArticle 类中放入一个名为privacy 的参数,然后以这种方式传递两个或三个。我只是想一口气做到这一点。
  • 我明白了。所以你是说在 linq to sql 中没有办法做到这一点 - 我应该使用上面的替代方法?

标签: c# asp.net-mvc linq-to-sql


【解决方案1】:

无法将该函数转换为SQL,一种解决方法是使用 linq to sql 进行大部分查询,其余部分使用 Linq to Objects。应该是这样的:

 var query = ( from a in n.articles
                       join s in n.sections on a.section equals s.section_id
                       join p in n.privacies on a.privacy equals p.privacy_id
                       where (a.active == true && a.article_id == id)
                       select new intranetGS.viewArticle {
                           articleId = a.article_id,
                           title = a.title,
                           author = a.author,
                           html = a.html,
                           section = s.name,
                           privacy = p.name,
                           privacyId = a.privacy,
                           dateCreated = a.date_created,
                           dateModified = a.date_modified,
                           userCreated = a.user_created,
                           userModified = a.user_modified
                       }).ToList();

然后过滤列表:

var result = query.Where(a => (a.privacyId < isInDept(a.section)) ||
                       (a.privacyId == 3 && a.author == user)).First();

然后你可以对第二个查询做同样的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多