如何围绕 linq 中的每条记录旋转并在视图中显示该记录
我的数据库中有四个表,我想加入它并返回记录并将其显示到 searchController 中!
我的查询是这样的:
public IQueryable PerformSearch(string query)
{
if (!string.IsNullOrEmpty(query))
{
var results = from tbl1 in context.Table1
join tbl2 in context.Table2 on tbl1.Id equals tbl2.Id
join tbl3 in context.Table3 on tbl2.Id equals tbl3.Id
join tbl4 in context.Table4 on tbl3.Id equals tbl4.Id
where tbl1.col2.Contains(query)
orderby tbl1.Count descending
select new
{
col1 = tbl1.Col1,
col1 = tbl1.Col1,
col1 = tbl1.Col1,
.
.
.
};
return results.AsQueryable();
}
else return null;
}
这个方法在 SearchController 中调用如下:
public class SearchController : System.Web.Mvc.Controller
{
public System.Web.Mvc.ActionResult Search(System.String query)
{
var search = new Search();
ViewData["result"] = search.PerformSearch(query);
return View("Search");
}
}
我不知道如何围绕 PeformSeach 方法返回的每条记录(加上与智能感知功能)进行旋转并在视图中显示!这也是个好办法吗?
提前致谢
I have four tables in my database and i want to join that's and return record's and show that into searchController!
My query is this:
public IQueryable PerformSearch(string query)
{
if (!string.IsNullOrEmpty(query))
{
var results = from tbl1 in context.Table1
join tbl2 in context.Table2 on tbl1.Id equals tbl2.Id
join tbl3 in context.Table3 on tbl2.Id equals tbl3.Id
join tbl4 in context.Table4 on tbl3.Id equals tbl4.Id
where tbl1.col2.Contains(query)
orderby tbl1.Count descending
select new
{
col1 = tbl1.Col1,
col1 = tbl1.Col1,
col1 = tbl1.Col1,
.
.
.
};
return results.AsQueryable();
}
else return null;
}
And this method called in SearchController as below:
public class SearchController : System.Web.Mvc.Controller
{
public System.Web.Mvc.ActionResult Search(System.String query)
{
var search = new Search();
ViewData["result"] = search.PerformSearch(query);
return View("Search");
}
}
I don't know how i can rotate around each record (plus vs intellisense feature) returned by PeformSeach method and show that in view! Also is this a good way?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议为此使用强类型视图。
首先从 PerformSearch 返回一个模型对象而不是 IQueryable。将此模型对象设置为您的视图。
一旦您的视图知道需要显示什么类型的数据,您就可以使用智能感知。
I would recommend using a strongly typed View for this.
First of all return a model object from PerformSearch instead of IQueryable. Set this model object to your View.
Once your View knows what kind of data it needs to display then you can use intellisense.