遍历ICollection以返回布尔值

时间:2018-07-31 17:48:53

标签: c# asp.net-mvc

我实质上是在创建博客(命名约定略有不同)。我在“发布”类(我称之为故事)上有一个属性,该属性与称为“可见性”的表相关联。帖子可以是公开的也可以是私有的。

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

我已经创建了一个视图模型:

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; }
}

在我的用户控制器中,当某人单击个人资料以查看个人资料的详细信息时,我从数据库中获取了该用户,获取了与该用户关联的所有故事(文章),并包含了与该文章相关的各种表格,获取帖子数,然后将这些值插入我的视图模型。这是通过以下代码完成的:

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);
}

我想做的是创建一个称为IsStoryPrivate的方法,该方法返回一个布尔值,并且需要遍历故事中的每个故事。然后将true / false值传递给IsStoryPrivate字段中的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";
    }
}

这也会导致错误。我不确定如何编写代码来遍历从数据库返回的故事列表,并为每个可以发送到视图模型的故事提供真假信息。

2 个答案:

答案 0 :(得分:5)

不是从数据库中获取所有故事,然后决定是否显示它们,而是对初始查询进行过滤:

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");
}

如果您正在加载Visibilty关系以进行支票,则可以现在将其省略:

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

答案 1 :(得分:0)

我不是100%地确定您理解正确。 如果您需要设置单个布尔值(例如“如果有任何故事是私有的,则将其设置为true”)

 var viewModel = new UserDetailsViewModel
{
    User = foundUser,
    IsStoryPrivate = stories.Any(x => IsStoryPrivate(x)), // or simply .Any(IsStoryPrivate)
    IsRegisteredUser = isRegisteredUser,
    Stories = stories,
    StoryCount = numOfStories,
    ReviewCount = numOfReviews
};

或者如果所有故事都是私人的,则需要设置它,则可以使用All代替Any