1

I am developing an application using ASP.Net MVC and AngularJS. I am not familiar with linq and honestly I don't like it. But I am very familiar with SQL and I can perform complex queries. So, my question is, I want you to look at the below code from my MVC controller:

public JsonResult GetSupervisor()
    {
        var db = new scaleDBEntities();
        return this.Json((from userObj in db.Users
                          select new
                          {
                              supervisorId = userObj.Id,
                              supervisorfName = userObj.usrFirstName,
                              supervisorlName = userObj.usrLastName,                                  
                          })
                          , JsonRequestBehavior.AllowGet
                        );
    }

How can I change the link query into SQL query?

I believe that I can do something like this:

var blogNames = db.Database.SqlQuery<string>("SELECT Name FROM dbo.Blogs").ToList();

So, if this is right, how can i use in in my return this.Json() for my angular?

5
  • 1
    Why don't you like LINQ? If I may ask Commented Jun 22, 2016 at 21:06
  • I did not like it, I am not used to it, I prefer SQL. I am using it in my application but I have very complex queries that I can perform with SQL but not with linq. Commented Jun 22, 2016 at 21:11
  • 1
    I don't understand why someone voted down this question? If this is not important to you (voter), it is to me!! Commented Jun 22, 2016 at 21:13
  • 1
    Remember that LINQ was invented for a reason: compile-time checking of queries. You loose that if you write raw SQL. In my opinion it's worth the effort to try and get used to writing LINQ and get the hang of manipulating it in a way that reasonable SQL is generated. It will never compete with hand-crafted SQL, which you can always use as a resort when LINQ does a poor job. Commented Jun 22, 2016 at 21:31
  • LINQ will help you solved your task in many occasion it is very useful. Commented Jun 23, 2016 at 3:31

1 Answer 1

1

Using the example you provided, something like this should work:

return this.Json(new {blogNames});

You're just creating an anonymously-typed object that the JSON serializer can use to produce an object like this:

{
  "blogNames": ["blog one", "blog two"]
}

It'll be more complicated if you're trying to produce more complex results from a more complex query. But, well, that's what an ORM is for. I'd echo Gert Arnold's advice to embrace LINQ, rather than just deciding you don't like it because you're not used to it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this was very helpful

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.