为了账号安全,请及时绑定邮箱和手机立即绑定

请教.net mvc 中linq如何实现这个功能?

请教.net mvc 中linq如何实现这个功能?

holdtom 2018-12-06 16:06:56
最近参考网上代码: using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.Linq;namespace WMVC.Models{ public class RepositoryTest<T> where T : class { public DataContext context; public RepositoryTest(DataContext context) { this.context = context; } public IEnumerable<T> FindAll(Func<T, bool> exp) { return context.GetTable<T>().Where(exp); } }}   可以实现:  DataContext content = new MyDataContext();RepositoryTest rp = new RepositoryTest<my_tbl>(DataContext);rp.FindAll(t => t.name == "jack"));return View(rp);       那么下面我想实现:  DataContext content = new MyDataContext();RepositoryTest rp = new RepositoryTest<my_tbl>(DataContext);rp.FindAll(1,t => t.name == "jack")); //这里多加了一个条件return View(rp);   我修改了代码如下,但是无法编译,请教各位前辈应该如何修改,谢谢! using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.Linq;namespace WMVC.Models{ public class RepositoryTest<T> where T : class { public DataContext context; public RepositoryTest(DataContext context) { this.context = context; } public IEnumerable<my_tbl> FindAll(int pid, Func<my_tbl, bool> exp) { var result = from a in context.GetTable<my_tbl>() where a.parent_id == pid select a; //result.Where(t=>t.name == "jack"); 可通过 //result.Where(exp); 出错 return result; } }}   实际方法定义代码如下: public IEnumerable<MenuClasses> listBom(int? id,Func<MenuClasses, bool> exp) { id = id == null ? 0 : id; SourceClassDataContext s = (SourceClassDataContext)context; var result = from a in s.GetTable<tbl_SourceClasses>() join b in s.class_f_Cid(id) on a.id equals b.id into prods from b in prods orderby b.temp_orderby select new MenuClasses() { id = a.id, parent_id = a.parent_id, class_order = a.class_order, class_name = a.class_name, temp_orderby = b.temp_orderby, Level = b.Level }; //result = result.Where(b => b.Level == 1); //可通过 //result = result.Where<MenuClasses>(exp); //无法通过 return result; }  
查看完整描述

添加回答

代码语言

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号