2

I want to query the table News from the database called StiriDB. There I want take all the entries that have in the Description field the word Stored in SearchTxt. I can't quite figure out what the SqlQuery method wants from me ... This is the code:

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. SearchTxt is taken with QueryString of Request because it's an URL attribute. 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);

4
  • 3
    No!!! DON'T create SQL query strings if you're using Linq-to-EF; you're losing all the benefits (compiled queries + parametric queries that prevent SQL injection). Use Linq syntax!! Commented Oct 25, 2014 at 11:10
  • Oh i don't really know , i am a newbie. And i am learning by myself so bits and pieces from here to there :D Commented Oct 25, 2014 at 11:20
  • No worries; go with Paul's approach: var query = from n in ctx.News.... and not queryTxt = "Select * from StiriDB.N..." Commented Oct 25, 2014 at 11:26
  • 1
    A small suggestion for SQL queries, if you would need to create SQL queries in the furture, try to use parameterized queries, it's much saver. Commented Oct 25, 2014 at 15:27

1 Answer 1

3

Use Linq to Entities query, something like...

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.