首先,抱歉我的英语不好。 在传统的ADO.Net中,我使用此函数进行搜索,并在GridView中显示行的DataTable或...
public DataTable GetTableData(string fieldName, string value)
{
DataTable ds = new DataTable();
SqlConnection scon = new SqlConnection("Data Source=.;Initial Catalog=ResearchPackDB;Integrated Security=True;");
scon.Open();
string str = string.Format("SELECT * FROM V_ScientificNews WHERE {0} LIKE {1}", fieldName, value);
SqlDataAdapter sda = new SqlDataAdapter(str, scon);
sda.Fill(ds);
scon.Close();
return ds;
}
现在我想用Linq Entity重写这个函数,函数就像这样!!
public List<V_ScientificNews> GetNews(string fieldName, string value)
{
var q = (from r in _entities.V_ScientificNews
select r)
.Where(?????????????);
return q.ToList();
}
如果有任何想法请告诉我。感谢
答案 0 :(得分:0)
您可以尝试以下内容:
public List<V_ScientificNews> GetNewsByFieldName(string value)
{
var q = (from r in _entities.V_ScientificNews
where r.FieldName.Contains(value)
select r);
return q.ToList();
}
或者这个:
public List<V_ScientificNews> GetNewsByFieldName(string value)
{
var q = _entities.V_ScientificNews.Where(x=>x.FieldName.Contains(value));
return q.ToList();
}
例如,如果您想获取科学新闻,其属性Name
包含value
,那么您可以声明如下方法:
public List<V_ScientificNews> GetNewsByName(string value)
{
var q = (from r in _entities.V_ScientificNews
where r.Name.Contains(value)
select r);
return q.ToList();
}