简体   繁体   中英

How can Convert traditional ADO.Net search to Linq Entity

first, sorry my bad English. in the traditional ADO.Net I used this function for search and got DataTable of rows to show in GridView or ...

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;
}

Now I want rewrite this function with Linq Entity, function like this!!

    public List<V_ScientificNews> GetNews(string fieldName, string value)
{
    var q = (from r in _entities.V_ScientificNews
                select r)
        .Where(?????????????);
    return q.ToList();
}

if have any Idea please tell me. thanks

You could try something like the following one:

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();
}

or this one:

public List<V_ScientificNews> GetNewsByFieldName(string value)
{
    var q = _entities.V_ScientificNews.Where(x=>x.FieldName.Contains(value));
    return q.ToList();
}

So for instance, if you want to get the scientific news, whose property called Name contains the value , then you could declare a method like the below:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM