【发布时间】:2017-01-30 11:08:32
【问题描述】:
我想获取 ID,但我只有名称。 我的代码如下所示:
var comments = new List<Comments>
{
new Comments{
CommunityId = community.FirstOrDefault(comid => comid.IdCommunity.Where(comid.CommunityName == "TestCommunity")),
}
};
评论是一个类:
public class Comments
{
public int IdComment { get; set; }
public DateTime Timestamp { get; set; }
public string Text { get; set; }
public int UserId { get; set; }
public int CommunityId { get; set; }
}
社区也是如此:
public class Community
{
public int IdCommunity { get; set; }
public string CommunityName { get; set; }
public Pictures Picture { get; set; }
}
但是 where 在 C# 中不被接受。 我需要做什么?
【问题讨论】:
-
定义不被接受。编译时出错?你有
using System.Linq;坐在上面吗? -
你能提供,什么是社区?
-
Where返回一个集合,但FirstOrDefault需要一个bool。您可能希望使用Any而不是Where或链接FirstOrDefault在Where之后。具体取决于community和comid是什么。 -
@PatrickHofman 是的,我已经使用 SystemLinq 进行了定义
-
@Abion47 好吧,我的错,我会更新我的问题。