2015-12-04 18 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()); 
      }