3

linq to sql クエリの where 句の最中に、public int に対してチェックしたいと思います。次のエラーが表示されます: メソッド 'Int32 isInDept(System.String)' には SQL への変換がサポートされていません。

漠然と関連するクラス (ad と呼ばれる public static クラスから):

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

私は何を間違っていますか?このようにできない場合、どのようにすればよいでしょうか?

4

1 に答える 1

3

その関数を に変換することはできません。SQLこれに対する 1 つの回避策は、ほとんどのクエリを 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();

次に、2 番目のクエリでも同じことができます。

于 2013-10-15T21:09:51.737 に答える