日期:2014-05-20  浏览次数:20752 次

关于EF 动态条件
pubilc list<c> getAllByWhere(string sqlWhere)
{
  var retruns = (from p in context.A
  from q in context.B
  where p.name = sqlWhere)
  select new c
  {

  }.toList();
}


大致就是这段程序 如果 sqlWhere == "" 红色部分不应该出现

这样 如何处理 谢谢各位拉  
 

------解决方案--------------------
给个示例

C# code


pubilc list<c> FindList(string name, int? type)
{
    using(var context = Factory.Create())
    {
        var query = context.A;
        if(name != null)
            query = query.Where(x => x.Name.Contains(name));
        if(type != null && type.HasValue)
            query = query.Where(x => x.Type == type.Value);
            
        var result = from p in query
                     from q in context.B where p.ID == q.ID
                     select ...

        return result.ToList();
    }
}