EF中Entity SQL用法

Posted 如果声音记得

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF中Entity SQL用法相关的知识,希望对你有一定的参考价值。

 public List<policecase> GetPoliceCaseList(PoliceCaseFilter view)
        {
            string sqlString = "SELECT value pc FROM Entities.policecase AS pc";
            var where = "";
            if (!string.IsNullOrWhiteSpace(view.CaseAddress))
            {
                //警情地址
                if (where != "")
                {
                    where += " and pc.[警情地址]  like ‘%" + view.CaseAddress + "%‘";
                }
                else
                {
                    where += " where pc.[警情地址]  like ‘%" + view.CaseAddress + "%‘";
                }             
            }
            if (!string.IsNullOrWhiteSpace(view.CaseType))
            {
                //警情类别
                if (where != "")
                {
                    where += " and pc.[警情类别] like ‘%" + view.CaseType + "%‘";
                }
                else
                {
                    where += " where pc.[警情类别] like ‘%" + view.CaseType + "%‘";
                }
            }
            if (!string.IsNullOrWhiteSpace(view.StartTime))
            {              
                //报警时间
                if (where != "")
                {
                    where += " and pc.[报警时间] >=datetime‘" + Convert.ToDateTime(view.StartTime).ToString("yyyy-MM-dd HH:mm:ss") + "‘";
                }
                else
                {
                    where += " where pc.[报警时间] >=datetime‘" + Convert.ToDateTime(view.StartTime).ToString("yyyy-MM-dd HH:mm:ss") + "‘";
                }
            }
            if (!string.IsNullOrWhiteSpace(view.EndTime))
            {
                //报警时间
                if (where != "")
                {
                    where += " and pc.[报警时间] <=datetime‘" + Convert.ToDateTime(view.EndTime).ToString("yyyy-MM-dd HH:mm:ss") + "‘";
                }
                else
                {
                    where += " where pc.[报警时间] <=datetime‘" + Convert.ToDateTime(view.EndTime).ToString("yyyy-MM-dd HH:mm:ss") + "‘";
                }
            }
            if (!string.IsNullOrWhiteSpace(view.PoliceStation))
            {
                //所属分局
                if (where != "")
                {
                    where += " and pc.[所属分局] like ‘%" + view.PoliceStation + "%‘";
                }
                else
                {
                    where += " where pc.[所属分局] like ‘%" + view.PoliceStation + "%‘";
                }
            }
            if (!string.IsNullOrWhiteSpace(view.RptType))
            {
                //报警类型
                if (where != "")
                {
                    where += " and pc.[报警类型] like ‘%" + view.RptType + "%‘";
                }
                else
                {
                    where += " where pc.[报警类型] like ‘%" + view.RptType + "%‘";
                }
            }

            sqlString += where;

            var objctx = (Repository as IObjectContextAdapter).ObjectContext;
            ObjectQuery<policecase> pc = objctx.CreateQuery<policecase>(sqlString);

            var data = pc.ToList();

            return data;
        }
    }















































































以上是关于EF中Entity SQL用法的主要内容,如果未能解决你的问题,请参考以下文章

Entity Framework——执行sql语句

.NET Entity Framework(EF)使用SqlQuery直接操作SQL查询语句或者执行过程

Entity Framework应用:Code First模式数据迁移的基本用法

EF 6.0 中缺少 DbSet<entity>.Load() 函数

Entity FrameWork和Dapper的使用

EF(Entity Framwork)结构