日期:2014-05-18  浏览次数:20653 次

急急急,Linq to sql多条件查询同一字段,参数均相同
string[] ll = { "PA", "@", "220","50Hz","通用动力总成"};

  IQueryable<OITM> query = db.OITM;
  //for (int i = 0; i < list.Count;i++ )
  foreach (string keyword in ll)
  {
  string temp = keyword;
  query = query.Where(p => p.ItemName.Contains(temp));
  }
  DbCommand cmd = db.GetCommand(query);
  string str = cmd.CommandText;
  foreach (DbParameter p in cmd.Parameters)
  {
  string pa = p.ParameterName;
  string va = p.Value.ToString();
  }

  return query.ToList();  
以上代码的查询条件都是针对ItemName,经过测试,发现,DbParameter 对应的Value均为"通用动力总成",这是什么原因呢?请高手帮忙看看

------解决方案--------------------
因为DbCommand cmd = db.GetCommand(query)
里面的query是根据ll中最后一个通用动力总成查询出来的
你可以把后面的代码放进foreach里面
C# code

IQueryable<OITM> query = db.OITM;
  //for (int i = 0; i < list.Count;i++ )
  foreach (string keyword in ll)
  {
      string temp = keyword;
      query = query.Where(p => p.ItemName.Contains(temp));
        DbCommand cmd = db.GetCommand(query);
      string str = cmd.CommandText;
     foreach (DbParameter p in cmd.Parameters)
     {
      string pa = p.ParameterName;
      string va = p.Value.ToString();
     }

  }

------解决方案--------------------
C# code
  string pa="";
  string va= "";
foreach (DbParameter p in cmd.Parameters)
  {
  pa += p.ParameterName + " ,";
  va += p.Value.ToString() + " ,";;
  }