MVC 5将SQL查询转换为EF Linq

时间:2016-04-19 15:41:48

标签: sql asp.net entity-framework linq asp.net-mvc-5

我有这个结果: enter image description here

问题在于我没有使用EF的力量。

我关注了this EF教程,但我现在能够做到。

ActivityLogController.cs

using System;
...

namespace AccountSystem.Controllers
{
    public class ActivityLogController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: ActivityLog
        public ActionResult Index()
        {
            /* var id = User.Identity.GetUserId();
            var Emaill = (from x in db.Users
                          where x.Id == id
                         select x.Email).Single(); */



            //string Access = AccessQ.ToString();

            IEnumerable<ActivityLogViewData> model = null;
            List<ActivityLogViewData> verify = new List<ActivityLogViewData>();
            SqlConnection connection = new SqlConnection("xxx");
            connection.Open();
            SqlCommand command = new SqlCommand("", connection);
            command.CommandText = "select dbo.AspNetUsers.Email,dbo.AspNetUsers.UserName,dbo.ActivityLogModels.ID,dbo.ActivityLogModels.Acess,dbo.AspNetUsers.Id from dbo.ActivityLogModels inner join dbo.AspNetUsers on dbo.ActivityLogModels.userID=dbo.AspNetUsers.Id";
            SqlDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {
                verify.Add(new ActivityLogViewData { Email = dr[0].ToString(), UserName = dr[1].ToString(), accessID = Convert.ToInt32(dr[2].ToString()), Acess = Convert.ToDateTime(dr[3].ToString()), userID = dr[4].ToString()});
            }
            connection.Close();
            //verify.Add(new ActivityLogViewData {Email = Emaill });
            //return View(db.ActivityLog.ToList());
            return View(verify);
        }

评论的行是我不成功的尝试。

有人可以将其转换为使用EF吗?

TIA!

0 个答案:

没有答案