代码之家  ›  专栏  ›  技术社区  ›  J.G.Sable

迭代ICollection以返回bool

  •  3
  • J.G.Sable  · 技术社区  · 7 年前

    我基本上是在创建一个博客(使用稍微不同的命名约定)。我的“post”类(我称之为故事)有一个属性,它与一个名为“visibility”的表绑定。帖子可以是公开的,也可以是私人的。

    当用户查看另一个成员的配置文件时,他们应该能够看到所有的公开帖子。

    我创建了一个视图模型:

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

    我要做的是创建一个名为IsStoreyDrive的方法,该方法返回一个布尔值,需要遍历故事中的每个故事。然后将true/false值传递到IsStorePrivate字段中的ViewModel。

    我尝试过使用此代码:

    public bool IsStoryPrivate(Story story)
    {
        return story.Visibility.Name == "Private";
    }
    

    然后尝试在控制器中调用它,但是失败了,因为我没有将单个Story对象传递给方法,而是传递给一个Stories集合或列表。

    然后我试了一下:

    public bool IsStoryPrivate(ICollection<Story> story)
    {
        foreach (story in story)
        {
            return Story.Visibility.Name == "Private";
        }
    }
    

    这也会导致错误。我不知道如何编写代码来遍历从数据库返回的故事列表,并为每个我可以发送到ViewModel的故事列表提供一个true/false。

    2 回复  |  直到 7 年前
        1
  •  5
  •   Camilo Terevinto Chase R Lewis    7 年前

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

    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);
    
        2
  •  0
  •   Denys Prodan    7 年前

    我不能百分之百地肯定你的理解是正确的。 如果需要设置单个bool值(例如“如果有任何故事是私有的,则将其设置为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 ,如果您需要设置它,如果所有的故事都是私有的