2014-06-12 5 views
1

MongoDb으로 작업 중이며 PredicateBuilder 클래스를 사용하여 where 절을 동적으로 C#으로 만듭니다. 그러나 예외가 생성됩니다 같은 :MongoDb C# 쿼리, where

[System.ArgumentException] { "지원되지 않는 where 절 :."},

및 동적 절은 사용

{c => (True AndAlso Invoke(c => (c.ID == value(ASP.search_aspx).txtid.Text), c))} , 

쿼리가 어디에 생성 :

var result = collection.AsQueryable<USER>() 
         .Where(where_clause) 
         .Select(c => new { c.ID, c.COMPANYNAME, c.EMAIL 
         }).Take(100).ToList(); 

컬렉션은 인스턴스입니다 MongoCollection

코드 WHERE_CLAUSE

VAR를 만들기위한 WHERE_CLAUSE = PredicateBuilder.True(); (ASP.search_aspx).txtid.Text 참조 오히려 현재 값을 평가하기보다는 폐쇄에 캡처 된 것처럼 때문에 Invoke

//object result=0; 
    if ((txtGlid.Text).Trim() != "") 
    { 
     where_clause = where_clause.And(c => c.GLUSR_USR_ID == txtGlid.Text); 
    } 
    if ((txtEmailid.Text).Trim() != "") 
    { 
     where_clause = where_clause.And(c => c.GLUSR_USR_EMAIL == txtEmailid.Text); 
    } 
    if ((txtPhone.Text).Trim() != "") 
    { 
     where_clause = where_clause.And(c => c.GLUSR_USR_PH_NUMBER == txtPhone.Text); 
    } 
    if ((txtMobile.Text).Trim() != "") 
    { 
     where_clause = where_clause.And(c => c.GLUSR_USR_PH_MOBILE == txtMobile.Text); 

    } 
+0

'where_clause'를 작성한 코드를 게시 할 수 있습니까? – StuartLC

답변

0

, 그것은 보인다. 어디 c.ID == 그것은 아마도 당신이`추가 한 것을 의미한다

if (txtGlid.Text.Trim() != "") 
{ 
    string s = txtGlid.Text; 
    where_clause = where_clause.And(c => c.GLUSR_USR_ID == s); 
} 
: 내가 엉망으로 만드는 식 파서의 기회를 방지하기 위해, 현지에서 명시 적으로 텍스트 상자에 값을 평가하도록 요청할 수 동적 빌더에서 'where c.ID == txtid.Text()`대신에'txtid.Text`를 술어로 사용합니다. 문자열 값을 전달하면 더 간단한 술어`(True AndAlso c.ID == "someLiteralValue")가 생성되어야합니다. 이는 Linq 제공자가 구문 분석 할 수 있기를 바랍니다.
+0

하지만 컬렉션이 목록 유형 인 경우 List와 마찬가지로 작동합니다. –

+1

또한 'Text'는 'txtid'의 속성이 아니며 –

+0

의견을 보내 주셔서 감사합니다.하지만 제안하지 않았지만 동일한 오류가 발생했습니다 ... where_clause = where_clause.And (c => c.GLUSR_USR_ID == "10387904"); –

0

AsExpandable()를 사용해보십시오 :

var result = collection.AsQueryable<USER>() 
         .AsExpandable() 
         .Where(where_clause) 
         .Select(c => new { c.ID, c.COMPANYNAME, c.EMAIL 
         }).Take(100).ToList(); 

AsExpandable()LinqKit에서 찾을 수 있습니다.

+0

컬렉션은 MongoCollection 유형이며, AsExpandable()을 지원하지 않습니다. –

+0

도움을 주셔서 감사합니다.하지만 LinqKit을 사용하여 이미 시도했습니다. –

+0

@ank_UD하지만 'IQueryable '에서 사용하고 있으므로 문제가되지 않습니다. 수동으로 where 절을 설정할 때 작동합니까? – Loetn