EF另一个 SqlParameterCollection 中已包含 SqlParameter。

代码:

SqlParameter[] commandParameters = new SqlParameter[]{
                new SqlParameter("@CultID",filters.ParentID)
            };

            var result = db.Database.SqlQuery<FM_PlantSolutions>(@"select s.* ,u.UserName as PrincipalName,
                    isnull(ue.UserName,'无') as ExaminerName ,i.BaseName AS InputsTypeName
                    from [v_PlantSolutionsRelation] as r
                    inner join dbo.FM_Solutions as s on r.SoluID = s.SoluID
                    inner join dbo.U_UserInfo as u on s.Principal= u.UserID
                    left join dbo.U_UserInfo as ue on s.Examiner = ue.UserID 
                    inner join dbo.B_BaseInfo as i on s.InputsType=i.BaseValue
                    where r.CultID = CultID and  i.ClassID = 21 ", commandParameters);
            var q= result.OrderBy(t => t.SoluName).Skip((filters.page - 1) * filters.rows).Take(filters.rows);

            return new 
            {
                rows = q.ToList(),
                records = result.Count(),

                page = filters.page,
                pagesize = filters.rows
            };

  

查询报表的时候需要通过两次查询取出数据。

第一次,用count(*)查出总数;

第二次,用rownumber分页取出想要的页内容;

然而,无论怎么修改,总是报错:另一个 SqlParameterCollection 中已包含 SqlParameter。
参照了网友的做法:https://www.cnblogs.com/jhli/p/6898650.html

但是并没有解决问题,分析报错的原理,大概是这段代码执行了两次,传入了两次同样的参数,所以索性将.ToList()往前放,直接将所有数据加载到内存中,再进行分页,问题解决了,修改后的代码
SqlParameter[] commandParameters = new SqlParameter[]{
                new SqlParameter("@CultID",filters.ParentID)
            };

            var result = db.Database.SqlQuery<FM_PlantSolutions>(@"select s.* ,u.UserName as PrincipalName,
                    isnull(ue.UserName,'无') as ExaminerName ,i.BaseName AS InputsTypeName
                    from [v_PlantSolutionsRelation] as r
                    inner join dbo.FM_Solutions as s on r.SoluID = s.SoluID
                    inner join dbo.U_UserInfo as u on s.Principal= u.UserID
                    left join dbo.U_UserInfo as ue on s.Examiner = ue.UserID 
                    inner join dbo.B_BaseInfo as i on s.InputsType=i.BaseValue
                    where r.CultID = CultID and  i.ClassID = 21 ", commandParameters).ToList();
            var q= result.OrderBy(t => t.SoluName).Skip((filters.page - 1) * filters.rows).Take(filters.rows);

            return new 
            {
                rows = q,
                records = result.Count(),

                page = filters.page,
                pagesize = filters.rows
            };
        }

 

posted @ 2018-08-28 10:51  南东  阅读(217)  评论(0编辑  收藏  举报