2015-12-04 16 views
0

我正在使用Linq.Dynamic,我有層列表(字符串Matricule,字符串名稱..)雖然有些層沒有Matricule,但是當運行LINQ在哪裏Matricule屬性,System.NullReferenceException:未將對象引用設置爲對象的實例是否被拋出? 這裏是代碼的細節:使用LINQ.Dynamic過濾空屬性拋出異常

public class Tier 
{ 
    public string Matricule{get;set;} 
    public string Nom{get;set;} 
    public string Prenom{get;set;} 
} 

public class FilterByPredicate 
    { 
     public BindingList<T> Filter(string propName,string propValue,bool fromFirst) 
      { 

       var predicateString = fromFirst 
           ? "{0}.ToString().ToUpper().StartsWith(@{1})" 
           : "{0}ToString().ToUpper().Contains(@{1})"; 

       return new BindingList<T>(_list.Where(string.Format(predicateString, propName, 0), propValue.ToUpper()).ToList()); 
      } 

    } 


in other part of my code i call.. 
    FilterByPredicate fbp = new FilterByPredicate(costumersList>); 
    textBox1_Changed+= 
    { 
     tiersBindingSource.DataSource =fbp .Filter("Matricule", textBox1.Text,true);// exception throwed 

//but this work fine-> var q = from c in costumersList where c.Matricule.ToUpper().StartsWith(textBox1.Text) select c; 
    }; 
+0

你看過[問]?你知道MCVE是什麼嗎? (如果沒有,請在提問前仔細閱讀*) – Amit

+0

對不起,我的英語不好 – saimmm0710

+0

你的英文很好。你的問題是*不是* – Amit

回答

0

我已經找到了解決辦法閱讀文檔後,相應的「開始使用」頁面,只需比較空這樣的:

public BindingList<T> Filter(string propName,string propValue,bool fromFirst) 
      { 

       var predicateString = fromFirst 
           ? "{0} != null and {0}.ToString().ToUpper().StartsWith(@{1})" 
           : "{0} != null and {0}.ToString().ToUpper().Contains(@{1})"; 

       return new BindingList<T>(_list.Where(string.Format(predicateString, propName, 0), propValue.ToUpper()).ToList()); 
      }