首页 新闻 赞助 找找看

一条Sql语句的linq写法

0
悬赏园豆:30 [已解决问题] 解决于 2014-08-22 17:30

有表Biz_SurveyTitle,表为树状结构,父结点 ParentId字段,现希望写如下查询,怎么写Linq

select question,(select count(1) from [Biz_SurveyTitle] b where b.parentid=a.id)
FROM [dbo].[Biz_SurveyTitle] a

问题补充:

var list = result.Sort(orderBy, isAsc).Skip((pageIndex - 1) * PageSize).Take(PageSize)
.Select(c => new SurveyModel
{
Id = c.Id,
Question = c.Question,
IsQuestion = c.IsQuestion,
CreateTime = c.CreateTime,
CreateBy = c.CreateBy,
ChildCount = c.Childrens.Count()
}).ToArray();

 

我自己写的是这样子的,其中Childrens就是它子集的一个集合,这样可以实现,但是每取一条记录的子结点数都会跑一次数据库查询,我想一次查询搞定,不要写Join,Groupby的写法

 

请参考该博客中的查询http://www.cnblogs.com/guomingfeng/p/mvc-ef-query.html

engtech的主页 engtech | 初学一级 | 园豆:149
提问于:2014-08-19 15:10
< >
分享
最佳答案
0

SQL可以改写为自连接。

LINQ也可以直接写自连接

想一次查询搞定,不要写Join,Groupby的写法?

 

数据库查询方式决定了,不管你LINQ怎么写,都不会是你所需要的“一次搞定”。

 

LINQ 写JOIN很轻松就能搞定,不知你在坚持为什么不用JOIN,性能?

不想写JOIN 可以在数据映射上加个自身映射自身的关联

这样就像楼上一样(他是A表关联B表,你可以直接A表关联A表)。

 

另附一句,你纠结的地方可能错了。

你可以用当前这种SQL,再写自连接实现一个,看看他们的查询计划。

 

实际是完全一样的。

收获园豆:30
cclient | 菜鸟二级 |园豆:264 | 2014-08-20 18:02

其实我纠结的是如下这篇博客里截图中查询数据的地方,我监视到的SQL语句并不是其中一样,不知我是否用错还是怎样,我知道join可以轻松实现的

http://www.cnblogs.com/guomingfeng/p/mvc-ef-query.html

engtech | 园豆:149 (初学一级) | 2014-08-20 20:27
其他回答(3)
0

可以先查询所有数据出来,然后用Select(x=>new{Count=list.Count(x1=>x1.parentId=x.id)})

幻天芒 | 园豆:37175 (高人七级) | 2014-08-19 16:34
0

你能说哈你想查那些东西吗

C_林先森 | 园豆:60 (初学一级) | 2014-08-19 16:40

var list = result.Sort(orderBy, isAsc).Skip((pageIndex - 1) * PageSize).Take(PageSize)
.Select(c => new SurveyModel
{
Id = c.Id,
Question = c.Question,
IsQuestion = c.IsQuestion,
CreateTime = c.CreateTime,
CreateBy = c.CreateBy,
ChildCount = c.Childrens.Count()
}).ToArray();

支持(0) 反对(0) engtech | 园豆:149 (初学一级) | 2014-08-19 16:51

我自己写的是这样子的,其中Childrens就是它子集的一个集合,这样可以实现,但是每取一条记录的子结点数都会跑一次数据库查询,我想一次查询搞定

支持(0) 反对(0) engtech | 园豆:149 (初学一级) | 2014-08-19 16:53
0

看看这样行不?

var list = result.Where(a => a.parentid=="")
                .Sort(orderBy, isAsc).Skip((pageIndex - 1) * PageSize).Take(PageSize)
                .Select(c => new SurveyModel
                {
                    Id = c.Id,
                    Question = c.Question,
                    IsQuestion = c.IsQuestion,
                    CreateTime = c.CreateTime,
                    CreateBy = c.CreateBy,
                    Childrens = c.Childrens.Count()>0?GetChildrens(c.parentid):null,
                });
public object GetChildrens(string parentid)
        {
            var list = result.Where(a => a.parentid == parentid)
                  .Sort(orderBy, isAsc).Skip((pageIndex - 1) * PageSize).Take(PageSize)
                  .Select(c => new SurveyModel
                  {
                      Id = c.Id,
                      Question = c.Question,
                      IsQuestion = c.IsQuestion,
                      CreateTime = c.CreateTime,
                      CreateBy = c.CreateBy,
                      Childrens = c.Childrens.Count() > 0 ? GetChildrens(c.parentid) : null,
                  });
        }

加入一个获取子节点的方法

苦B程序员 | 园豆:74 (初学一级) | 2014-08-19 17:36

http://www.cnblogs.com/guomingfeng/p/mvc-ef-query.html

 

支持(0) 反对(0) engtech | 园豆:149 (初学一级) | 2014-08-19 17:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册