[英]Entity Framework SQL statement
I want to query the table News
from the database called StiriDB
. 我想从名为StiriDB
的数据库查询表News
。 There I want take all the entries that have in the Description
field the word Stored
in SearchTxt
. 我要在其中获取在Description
字段中SearchTxt
Stored
的单词的所有条目。 I can't quite figure out what the SqlQuery
method wants from me ... This is the code: 我不太清楚SqlQuery
方法向我要什么...这是代码:
public IQueryable<News> GetProducts()
{
var _db = new SiteStiri.Models.NewsContext();
String SearchTxt = Convert.ToString(Request.QueryString["Cauta"]);
String queryTxt = "Select * from StiriDB.News where Description like '%" + SearchTxt + "%'";
IQueryable<News> query = _db.News.SqlQuery<News>(queryTxt);
if ("DesDate".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderByDescending(u => u.ReleaseDate);
}
if ("AsDate".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderBy(u => u.ReleaseDate);
}
if ("AsAlp".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderBy(u => u.NewsTitle);
}
if ("DesApl".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderByDescending(u => u.NewsTitle);
}
return query;
}
Additional details : GetProducts
is called by a ListView. 其他详细信息: GetProducts
由ListView调用。 SearchTxt
is taken with QueryString
of Request
because it's an URL attribute. SearchTxt
与Request
QueryString
,因为它是一个URL属性。 The many ifs are for sorting the data in ascending and descending order based on certain criteria (the ifs work, I just need the SqlQuery
to work as intended); 许多if用于根据某些条件按升序和降序对数据进行排序(if可以正常工作,我只需要SqlQuery
即可按预期工作);
Use Linq to Entities query, something like... 使用Linq to Entities查询,类似...
public IQueryable<News> GetProducts()
{
var ctx = new SiteStiri.Models.NewsContext();
var query = from n in ctx.News
where n.Description.Contains(SearchTxt)
select n;
if ("DesDate".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderByDescending(u => u.ReleaseDate);
}
else if ("AsDate".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderBy(u => u.ReleaseDate);
}
else if ("AsAlp".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderBy(u => u.NewsTitle);
}
else if ("DesApl".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderByDescending(u => u.NewsTitle);
}
return query;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.