LINQ 动态查询(多条件)

热恋灬绝情 2014-10-29 03:06:31
小弟,没有接触过linq,现在项目需要一个linq查询,还有点复杂。求大神指教。
现有一个List<Marker> list 数据源,
Marker 有(id ,type【type1,type2,type3....】,
status【status1,status2,status3.....】 三个属性),

根据List<string> typeList 和List<string> statusList 进行过滤(两个list可能只有一个,可能都存在,所以需要动态查询)。

例如typeList = {type1,type3}; statusList ={status1,status4}

或者 typeList 为空 statusList ={status1}
过滤List<Marker> list 数据源得到 resultList,怎么求得这个resultList。

代码需要这样形式的:
ParameterExpression c = Expression.Parameter(typeof(Customer), "c");  
Expression condition = Expression.Constant(false);
foreach (string s in starts)
{
Expression con = Expression.Call(
Expression.Property(c, typeof(Customer).GetProperty("CustomerID")),
typeof(string).GetMethod("StartsWith",
new Type[] { typeof(string) }),
Expression.Constant(s));
condition = Expression.Or(con, condition);
}
Expressionbool>> end =
Expression.Lambdabool>>
(condition, new ParameterExpression[] { c });
...全文
1618 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggh_1766838097 2014-11-02
  • 打赏
  • 举报
回复
LINQ查询结果绑定到DataList控件示例 //DataList控件设置 <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <table align="center" cellpadding="0" cellspacing="0" width="543"> <tr> <td class="huise"> <span class="shenlancu">『<%# DataBinder.Eval(Container.DataItem,"type") %>』</span><span class="banhui"><%# DataBinder.Eval(Container.DataItem,"title") %> <br /> </span><span class="banhui">  <%# DataBinder.Eval(Container.DataItem,"info") %> <br /> </span><span class="chengse">  联系人:<%#DataBinder.Eval(Container.DataItem,"linkMan") %>    联系电话:<%#DataBinder.Eval(Container.DataItem,"tel") %></span></td> </tr> <tr style="color: #000000"> <td align="center"> <img height="1" src="images/longline.gif" width="525" /></td> </tr> <tr style="color: #000000"> <td height="10"> </td> </tr> </table> </ItemTemplate> </asp:DataList> //查询结果绑定 protected void Page_Load(object sender, EventArgs e) { LinqDBDataContext lqDB = new LinqDBDataContext(ConfigurationManager.ConnectionStrings["db_LinQConnectionString1"].ConnectionString.ToString()); //创建查询表达式 var result = from u in lqDB.tb_info where u.ID > 1 select new { Type = u.type, Title = u.title, Info = u.info, LinkMan = u.linkman, Tel=u.tel }; //绑定查询结果 DataList1.DataSource = result; DataList1.DataBind(); }
threenewbee 2014-10-29
  • 打赏
  • 举报
回复
贴出你的数据和预期的结果。 如果你不能写Lambda,可以写SQL,帮你改写。 如果你能写出Lambda,但是不会写表达式树,你可以先写好Lambda。帮你改写。
蝶恋花雨 2014-10-29
  • 打赏
  • 举报
回复
http://blog.csdn.net/kongwei521/article/details/27197753 参考我的。应该和我这个类似。

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧