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

这个SQL换成linq怎么写啊
string sql = "";
  sql = "select * from Tasks where 1=1";
  if (!string.IsNullOrEmpty(ddlpriority.SelectedValue))
  {
  sql += sql +"and id="+ Convert.ToInt32(ddlpriority.SelectedValue);
  }
-------------------------------------------
  NPMSLinqDataContext ctxt = new NPMSLinqDataContext();
  string linqstring=string.Empty;
  linqstring="";
  int _priority = 0;
  int _status = 0;
  DateTime _objtime;
  if (!string.IsNullOrEmpty(ddlpriority.SelectedValue))
  {
  _priority = Convert.ToInt32(ddlpriority.SelectedValue);
  }
  var q = from c in ctxt.Tasks
  where 1=1 
  && c.priority == (_priority == 0 ? : _priority) 这个地方怎么写啊 
  .........  
  select c;
   
  repeaterReportInfo.DataSource = q;
  repeaterReportInfo.DataBind();
  }
 第一次用LINQ 做项目 不要见笑

------解决方案--------------------
??没明白,你可以在前面这样写
C# code
_priority=_priority==0?0:_priority;
var qry=from c in ctxt.Tasks where 1=1 && c.priority==_priority select c;

------解决方案--------------------
var p= from c in ctxt.Tasks select c;
if (!string.IsNullOrEmpty(ddlpriority.SelectedValue)) 
p= p.Where(s => s._priority == ddlpriority.SelectedValue);

IQueryable<T> query = from c in ctxt.Tasks
where (c.priority == null || c.priority ==_priority ) 
select c;

------解决方案--------------------
C# code

IQueryable<Tasks> v = from t in Tasks
        select t;
if (!string.IsNullOrEmpty(ddlpriority.SelectedValue)) 
{
   v.Where (p=>p.priority == ddlpriority.SelectedValue);
}

------解决方案--------------------
C# code
IQueryable<Tasks> v = from t in Tasks
select t;
if (!string.IsNullOrEmpty(ddlpriority.SelectedValue)) 
{
v.Where (p=>p.priority == ddlpriority.SelectedValue);
}


------解决方案--------------------
d
------解决方案--------------------
提供表结构
测试信息
极其想要的结果
我帮你用sql写
------解决方案--------------------
也开始接触linq了,有很多都不会转
------解决方案--------------------
探讨
var p= from c in ctxt.Tasks  select c;
if (!string.IsNullOrEmpty(ddlpriority.SelectedValue))
    p= p.Where(s => s._priority == ddlpriority.SelectedValue);

IQueryable <T> query = from c in ctxt.Tasks 
where (c.priority == null ||  c.priority ==_priority )
select c;