C# 方法“”不支持到SQL的转换

C# 方法“”不支持到SQL的转换,c#,asp.net-mvc,linq-to-sql,C#,Asp.net Mvc,Linq To Sql,我想在linq to sql查询where子句中,对照公共int进行检查。我得到了以下错误:方法“Int32 isInDeptSystem.String”不支持到sql的转换 来自名为ad的公共静态类的模糊相关类: //get AD property public static string GetProperty(this Principal principal, String property) { DirectoryEntry directoryEntry

我想在linq to sql查询where子句中,对照公共int进行检查。我得到了以下错误:方法“Int32 isInDeptSystem.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,解决方法之一是使用linq to SQL进行大部分查询,其余部分使用linq to对象。应该是这样的:

 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();

然后,您可以对第二个查询执行相同的操作。

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