C# 迭代ICollection以返回bool

C# 迭代ICollection以返回bool,c#,asp.net-mvc,C#,Asp.net Mvc,我基本上是在创建一个博客(命名约定略有不同)。我的“post”类(我称之为story)上有一个属性,它绑定到一个名为“visibility”的表上。帖子可以是公共的,也可以是私人的 当用户查看其他成员的个人资料时,他们应该能够看到所有的公开帖子 我已经创建了一个viewmodel: public class UserDetailsViewModel { public bool IsRegisteredUser { get; set; } //public bool IsStory

我基本上是在创建一个博客(命名约定略有不同)。我的“post”类(我称之为story)上有一个属性,它绑定到一个名为“visibility”的表上。帖子可以是公共的,也可以是私人的

当用户查看其他成员的个人资料时,他们应该能够看到所有的公开帖子

我已经创建了一个viewmodel:

public class UserDetailsViewModel
{
    public bool IsRegisteredUser { get; set; }
    //public bool IsStoryPrivate { get; set; }
    public int StoryCount { get; set; }
    public int ReviewCount { get; set; }
    public ApplicationUser User { get; set; }
    public virtual IEnumerable<Story> Stories { get; set; }
}
我想做的是创建一个名为IsStoryPrivate的方法,该方法返回一个布尔值,并需要遍历故事中的每个故事。然后将真/假值传递给IsStoryPrivate字段中的viewModel

我已尝试使用以下代码:

public ActionResult Details(string id)
{
    //verify an id was passed 
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    //if an id was given as a parameter, find the user associated with that id
    var foundUser = dbContext.Users.SingleOrDefault(x => x.Id == id);

    //verify a user was found
    if (foundUser == null)
    {
        return HttpNotFound();
    }

    var isRegisteredUser = IsRegisteredUser(foundUser);

    //if a user was found, get all stories associated with the foundUser
    var stories = dbContext.Stories
        .Include("Genre")
        .Include("StoryType")
        .Include("StoryAgeRange")
        .Include("Visibility")
        .Where(x => x.AuthorId == foundUser.Id);

    var reviews = dbContext.Reviews.Where(x => x.ReviewerId == foundUser.Id);

    int numOfStories = stories.Count();
    int numOfReviews = reviews.Count();

    //create the viewmodel
    var viewModel = new UserDetailsViewModel
    {
        User = foundUser,
        //IsStoryPrivate = isStoryPrivate,
        IsRegisteredUser = isRegisteredUser,
        Stories = stories,
        StoryCount = numOfStories,
        ReviewCount = numOfReviews
    };

    return View(viewModel);
}
public bool IsStoryPrivate(Story story)
{
    return story.Visibility.Name == "Private";
}
然后尝试在控制器中调用它,但失败了,因为我没有将单个故事对象传递到方法中,而是传递一个故事集合或故事列表

然后我试了一下:

public bool IsStoryPrivate(ICollection<Story> story)
{
    foreach (story in story)
    {
        return Story.Visibility.Name == "Private";
    }
}
public bool IsStoryPrivate(ICollection故事)
{
foreach(故事中的故事)
{
return Story.Visibility.Name==“Private”;
}
}

这也会导致错误。我不知道如何编写代码来迭代从db返回的故事列表,并为每个故事提供一个true/false,我可以将其发送到viewmodel。

与其从数据库获取所有故事,然后决定是否显示它们,不如对初始查询进行筛选:

var stories = dbContext.Stories
    .Include("Genre")
    .Include("StoryType")
    .Include("StoryAgeRange")
    .Include("Visibility")
    .Where(x => x.AuthorId == foundUser.Id);

// filter for public stories only when the author is not the current user
if (!isRegisteredUser)
{
    stories = stories.Where(x => x.Visibility.Name == "Public");
}
如果您正在加载检查的
visibility
关系,现在可以忽略它:

var stories = dbContext.Stories
    .Include("Genre")
    .Include("StoryType")
    .Include("StoryAgeRange")
    .Where(x => x.AuthorId == foundUser.Id);

我不能百分之百肯定你的理解是正确的。 如果需要设置单个bool值(如“如果任何故事是私有的,则将其设置为true”)


或者您可以使用
All
而不是
Any
,如果您需要设置它,那么如果所有故事都是私有的

,您可能需要澄清您试图用布尔值确定的确切内容:所有故事都是公共的,至少一个公共的?如果需要,您可以筛选私人故事:
var privateStories=stories.Where(story=>story.Visibility.Name==“private”)我实际上不想这样做,因为我需要在razor视图中使用if/else。因此,如果登录用户与他们正在查看的个人资料是同一个人(意味着他们正在查看自己的个人资料),那么他们应该能够看到他们所有的帖子-公开和私人。但如果用户不是在看自己的个人资料,而是在看其他人的个人资料,他们应该只看到此人的公开帖子。这就是为什么我在那里有一个IsRegisteredUser bool,它将登录用户的ID检查到正在查看的用户配置文件。@J.G.Sable您不需要将此类工作转储到与控制器对应的视图中。查看我的updateGood解决方案!谢谢。@Camiloteriento您可能不需要再包括可见性,因为您知道它们是公开的
 var viewModel = new UserDetailsViewModel
{
    User = foundUser,
    IsStoryPrivate = stories.Any(x => IsStoryPrivate(x)), // or simply .Any(IsStoryPrivate)
    IsRegisteredUser = isRegisteredUser,
    Stories = stories,
    StoryCount = numOfStories,
    ReviewCount = numOfReviews
};