2013-10-15 69 views
2

我想在linq的sql查詢where子句中檢查public int。我得到這個錯誤:方法'Int32 isInDept(System.String)'沒有支持到SQL的轉換。方法''沒有支持轉換爲SQL

依稀相關的類(從公共靜態類稱爲廣告):

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

我在做什麼錯?如果我不能這樣做,它是如何被建議完成的?

+1

那麼您希望將該方法轉換爲什麼SQL? – Servy

+0

我想要確定isInDept()。最終的問題是,隱私級別可能是3(如果用戶不在給定部門)或2(如果用戶在給定部門)。我想我不完全理解這個問題。 – shubniggurath

+1

寫出希望將查詢翻譯成的SQL。就這麼簡單。我看不到任何可能的方式來處理那些代碼。很明顯,linq到sql查詢提供者都不是。 – Servy

回答

2

將函數轉換爲SQL是不可能的,一種解決方法是使用linq將大部分查詢轉化爲sql,然後將Linq用於其他對象。它應該是這樣的:

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

然後你可以在第二個查詢這樣做。

相關問題