我參考以下這一篇文章,想要做所謂的Dynamic LINQ:
http://kevintsengtw.blogspot.com/2012/06/dynamic-linq-entity-framework-part3.html
public ActionResult Test1()
using (NorthwindEntities db = new NorthwindEntities())
var query = db.Customers
.Where("City == @0 and Orders.Count >= @1", "London", 10)
.OrderBy("CustomerID desc")
.Take(10);
ViewData.Model = query.ToList();
return View();
然後我就把我的程式改寫成以下
public IPagedList<MemberViewModel> IPGetMemberToPaperRole(int ConferenceId, int PaperId, string Role, int? Page, string SearchKeyWord = "")
List<MemberViewModel> mv = new List<MemberViewModel>();
db.MemberToPapers.Where("ConferenceId == @0 AND PaperId == @1 AND Role == @2", ConferenceId, PaperId, Role)
.Join(db.Members.Where(wm => wm.Name.Contains(@3), SearchKeyWord),
m => m.MemberId,
x => x.MemberId,
(m, x) => new
m.ConferenceId,
m.PaperId,
m.Role,
x.MemberId,
x.Name,
x.Email
}).ToList()
.ForEach(o => mv.Add(new MemberViewModel
ConferenceId = o.ConferenceId,
PaperId = o.PaperId,
MemberId = o.MemberId,
Name = o.Name,
Email = o.Email,
RoleName = GetRole(o.Role)
int pageNumber = (Page ?? 1);
int pageSize = 2;
return mv.ToPagedList(pageNumber, pageSize);
VS回報錯誤:'方法'Where'沒有任何多載使用4個引數'
不知道要如何修正才能改為Dynamic LINQ呢?
我想要做的是若SearchKeyWord有值,則搜尋出KeyWord相關的資料,若SearchKeyWord沒值,則搜尋出全部的資料。
dynamic LINQ專案是開源的,可以看一下它使用的是DynamicExpression技術
所以有一個很重要概念它不只能用在LINQ SQL,普通的集合物件LINQ Object也可以使用,這是一個很強大的功能,舉例:
var datas = new[] { new { id = 1, value = "001" }, new { id = 2, value = "002" } };
var result = datas.AsQueryable().Where("id=@0", 1);
Console.WriteLine(result);
如大大建議,是不用Dynamic Linq,改天再試做Dynamic Linq,不過目前改寫的內容只能用最笨的寫法如下:
if (!string.IsNullOrEmpty(SearchKeyWord))
db.MemberToPapers.Where(x => x.ConferenceId == ConferenceId && x.PaperId == PaperId && x.Role == Role)
.Join(db.Members.Where(wm => wm.Name.Contains(SearchKeyWord)),
m => m.MemberId,
x => x.MemberId,
(m, x) => new
m.ConferenceId,
m.PaperId,
m.Role,
x.MemberId,
x.Name,
x.Email
}).ToList()
.ForEach(o => mv.Add(new MemberViewModel
ConferenceId = o.ConferenceId,
PaperId = o.PaperId,
MemberId = o.MemberId,
Name = o.Name,
Email = o.Email,
RoleName = GetRole(o.Role)
db.MemberToPapers.Where(x => x.ConferenceId == ConferenceId && x.PaperId == PaperId && x.Role == Role)
.Join(db.Members,
m => m.MemberId,
x => x.MemberId,
(m, x) => new
m.ConferenceId,
m.PaperId,
m.Role,
x.MemberId,
x.Name,
x.Email
}).ToList()
.ForEach(o => mv.Add(new MemberViewModel
ConferenceId = o.ConferenceId,
PaperId = o.PaperId,
MemberId = o.MemberId,
Name = o.Name,
Email = o.Email,
RoleName = GetRole(o.Role)
嘗試著用fillano大大建議的方法,遇到許多轉型的問題。。。><
var result = db.MemberToPapers.Where(x => x.ConferenceId == ConferenceId && x.PaperId == PaperId && x.Role == Role);
if (!string.IsNullOrEmpty(SearchKeyWord))
result = result.Join(db.Members.Where(wm => wm.Name.Contains(SearchKeyWord)),
m => m.MemberId,
x => x.MemberId,
(m, x) => new
m.ConferenceId,
m.PaperId,
m.Role,
x.MemberId,
x.Name,
x.Email
result = result.Join(db.Members,
m => m.MemberId,
x => x.MemberId,
(m, x) => new
m.ConferenceId,
m.PaperId,
m.Role,
x.MemberId,
x.Name,
x.Email
result.ToList()
.ForEach(o => mv.Add(new MemberViewModel
ConferenceId = o.ConferenceId,
PaperId = o.PaperId,
MemberId = o.MemberId,
Name = o.Name,
Email = o.Email,
RoleName = GetRole(o.Role)