首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

这个SQL换成linq如何写

2012-01-23 
这个SQL换成linq怎么写啊string sql sql select *from Tasks where 11if (!string.IsNullOrEmp

这个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;

热点排行