首页 新闻 会员 周边 捐助

关于linq FirstOrDefault()的问题

0
悬赏园豆:10 [已解决问题] 解决于 2013-12-10 13:33
 1  var test = (from comment in commentListContext.Comments
 2              group comment by comment.CommentActivityID into c
 3              select new
 4              {
 5                 activityId = c.Key,
 6                 commentInfo = (from comment2 in c
 7                                from activity in commentListContext.Activities
 8                               where (comment2.CommentActivityID == activity.ActivityID && 
                        comment2.CommentTime == c.Max(comment3 => comment3.CommentTime)) 10 select new 11 { 12 activityTitle = activity.ActivityTitle, 13 userId = comment2.CommentUserID, 14 commentId = comment2.CommentID, 15 commentContent = comment2.CommentContents, 16 17 }).FirstOrDefault(), 18 }).ToList();   var test2 = from commentlist in test 20 select new 21 { 22 activityId = commentlist.activityId, 23 activityTitle = commentlist.commentInfo.activityTitle, 24 commentId = commentlist.commentInfo.commentId, 25 }; 26 27 var test3 = (from comment in commentListContext.Comments 28 group comment by comment.CommentActivityID into c 29 select new 30 { 31 activityId = c.Key, 32 commentInfo = from comment2 in c 33 from activity in commentListContext.Activities 34 where (comment2.CommentActivityID == activity.ActivityID &&
                       comment2.CommentTime == c.Max(comment3 => comment3.CommentTime)) 36 select new 37 { 38 activityTitle = activity.ActivityTitle, 39 userId = comment2.CommentUserID, 40 commentId = comment2.CommentID, 41 commentContent = comment2.CommentContents, 43 }, 44 }).ToList(); 45 var test4 = from commentlist in test3 46 select new 47 { 48 activityId = commentlist.activityId, 49 //activityTitle = commentlist.commentInfo.activityTitle, 50 //commentId = commentlist.commentInfo.commentId, 51 }; 52 var test5 = from commentlist in test3 53 select new 54 { 55 activityId = commentlist.activityId, 56 commentInfo = from c in commentlist.commentInfo 57 select c, 58 };

 

var test = (from comment in commentListContext.Comments
group comment by comment.CommentActivityID into c
select new
{
activityId = c.Key,
commentInfo = (from comment2 in c
from activity in commentListContext.Activities
where (comment2.CommentActivityID == activity.ActivityID &&
comment2.CommentTime == c.Max(comment3 => comment3.CommentTime))
select new
{
activityTitle = activity.ActivityTitle,
userId = comment2.CommentUserID,
commentId = comment2.CommentID,
commentContent = comment2.CommentContents,

}).FirstOrDefault(),
}).ToList();
var test2 = from commentlist in test
select new
{
activityId = commentlist.activityId,
activityTitle = commentlist.commentInfo.activityTitle,
commentId = commentlist.commentInfo.commentId,
};

var test3 = (from comment in commentListContext.Comments
group comment by comment.CommentActivityID into c
select new
{
activityId = c.Key,
commentInfo = from comment2 in c
from activity in commentListContext.Activities
where (comment2.CommentActivityID == activity.ActivityID &&
comment2.CommentTime == c.Max(comment3 => comment3.CommentTime))
select new
{
activityTitle = activity.ActivityTitle,
userId = comment2.CommentUserID,
commentId = comment2.CommentID,
commentContent = comment2.CommentContents,

},
}).ToList();
var test4 = from commentlist in test3
select new
{
activityId = commentlist.activityId,
activityTitle = commentlist.commentInfo.activityTitle,//这里是错的
commentId = commentlist.commentInfo.commentId,//这里是错的
};
var test5 = from commentlist in test3
select new
{
activityId = commentlist.activityId,
commentInfo = from c in commentlist.commentInfo
select c,
};

test3没有用FirstOrDefault(),在test4那里就不能像test2那样的写法了,只能写成test5那样,请问下,这是什么原因啊?是什么原理呢?谢谢各位了。

wenbolwm的主页 wenbolwm | 初学一级 | 园豆:131
提问于:2013-12-05 13:25
< >
分享
最佳答案
0

activityTitle = commentlist.commentInfo ? null:commentlist.commentInfo.activityTitle,//这里是错的
commentId = commentlist.commentInfo ? null:commentlist.commentInfo.commentId,//这里是错的

收获园豆:10
Launcher | 高人七级 |园豆:45050 | 2013-12-05 13:31

test3没有用FirstOrDefault(),在test4,commentlist.commentInfo.activityTitle就没有这样的属性的

wenbolwm | 园豆:131 (初学一级) | 2013-12-05 13:46

@wenbolwm: 你是说 test4 中的 commentlist 没有 commentInfo 属性吗?

Launcher | 园豆:45050 (高人七级) | 2013-12-05 13:52

@Launcher: 没有commentInfo.后面的属性了

wenbolwm | 园豆:131 (初学一级) | 2013-12-05 13:54

@wenbolwm: 在 test4 中你的  commentlist 的 commentInfo 是 IQueryable<dynamic type> 类型的,

activityTitle = commentlist.commentInfo.FirstOrDefault() == null ?

null : commentlist.commentInfo.FirstOrDefault().activityTitle;

Launcher | 园豆:45050 (高人七级) | 2013-12-05 13:59

@Launcher: 这样可以点出属性了,能告诉下他们有什么区别?为什么这样?原理是什么吗?谢谢了

wenbolwm | 园豆:131 (初学一级) | 2013-12-05 14:03

@wenbolwm: 楼下已经告诉你了,你的 test3 中的是这样的: commentInfo = from comment2 in c

这里的 commentInfo 表示一个 IQueryable<dynamic type> 类型,而不是 dynamic type 类型。

Launcher | 园豆:45050 (高人七级) | 2013-12-05 14:08
其他回答(2)
0

test3没有用FirstOrDefault(),那么test3中的commentInfo是一个实体的List集合.

在Test1中commentInfo是一个实体.

//activityTitle = commentlist.commentInfo.activityTitle,

这个commentlist.commentInfo是一个集合

平常心队长 | 园豆:1113 (小虾三级) | 2013-12-05 13:53

是不是可以认为,实体后面可以点出属性来,集合就不行了

支持(0) 反对(0) wenbolwm | 园豆:131 (初学一级) | 2013-12-05 14:05

@wenbolwm: 你这是乱理解。假设:

class CommentInfo {public string ActivityTitle{get;set;}};

List<CommentInfo> list = new List<CommentInfo>();

list.ActivityTitle;   // 这能编译通过吗?

list[0].ActivityTitle; // 这能编译通过吗?

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2013-12-05 14:10

@Launcher: list[0].ActivityTitle 这个能编译通过, 理解了,谢谢你

支持(0) 反对(0) wenbolwm | 园豆:131 (初学一级) | 2013-12-05 14:19
0

test3没有用FirstOrDefault(),那么test3中的commentInfo是一个实体的List集合.

在Test1中commentInfo是一个实体.

幕三少 | 园豆:1384 (小虾三级) | 2013-12-05 15:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册