- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我实际上是在创建一个博客(命名约定略有不同)。我的“post”类(我称之为故事)有一个属性,它与一个名为“visibility”的表相关联。帖子可以是公开的也可以是私有(private)的。
当用户查看另一个成员的个人资料时,他们应该能够看到所有公开的帖子。
我已经创建了一个 View 模型:
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; }
}
在我的用户 Controller 中,当有人点击个人资料查看个人资料的详细信息时,我从数据库中获取用户,获取与该用户关联的所有故事(帖子)并包括与帖子相关的各种表格,获取帖子数量,并将这些值插入我的 View 模型。这是用这段代码完成的:
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 的方法,它返回一个 bool 值并且需要遍历故事中的每个故事。然后将 true/false 值传递给 IsStoryPrivate 字段中的 viewModel。
我试过这段代码:
public bool IsStoryPrivate(Story story)
{
return story.Visibility.Name == "Private";
}
然后尝试在 Controller 中调用它但失败了,因为我没有将单个故事对象传递给该方法,而是传递了一个集合 - 或者故事列表。
然后我试了一下:
public bool IsStoryPrivate(ICollection<Story> story)
{
foreach (story in story)
{
return Story.Visibility.Name == "Private";
}
}
这也会导致错误。我不确定如何编写代码来遍历从数据库返回的故事列表,并为我可以发送到 View 模型的每个故事提供 true/false。
最佳答案
不是从数据库中获取所有故事然后决定是否显示它们,而是对初始查询进行过滤:
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);
关于c# - 遍历 ICollection 返回一个 bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51619260/
注意:这与 this other question 相似,但不完全相同 我已经实现了一个 IBusinessCollection界面。它来自 ICollection ,以及旧的非通用 ICollect
配置 AutoMapper 以映射的最佳/最简单方法是什么 ICollection至 ICollection至 ICollection ? 我有一个如下所示的 DomainModel: public
只想对syntactic sygar做简单的扩展: public static bool IsNotEmpty(this ICollection obj) { return ((obj !=
这个问题在这里已经有了答案: Why does IEnumerable inherit from IEnumerable? (4 个回答) 8年前关闭。 IEnumerable实现 IEnumerab
我正在尝试编写一个查询,从我的连接数据中获取国家列表。 Places是List . var zonedCountries = (from dz in db.DeliveryZones.
我有一组消息,每个消息定义为: namespace DatabaseDesign.Models { public class PrivateMessageDetail {
C# 中的 ICollection 中是否有一些方法可以添加另一个集合的所有元素?现在我必须始终为此编写 foreach 循环: ICollection allLetters = ... //some
转换 ICollection 的推荐方式是什么?至 ICollection其中 Bar工具 IBar ? 是不是就这么简单 collection = new List(); ICollection =
我用网格创建了表单来可视化任何集合( ICollection , ICollection )对象。 之后我创建了调试器可视化类(从 Microsoft.VisualStudio.DebuggerVis
我正在尝试基于 Stack 创建自定义集合.当我看 Stack [来自元数据] 在 Visual Studio 中,它显示 Stack实现 ICollection ,这将要求它实现 ICollecti
例如: public interface IFoo { //... ICollection Children { get; } //... } public class Foo
这个问题在这里已经有了答案: Why is it considered bad to expose List? [duplicate] (6 个答案) 关闭 9 年前。 我正在使用 FxCop 工具
这个问题在这里已经有了答案: Why does List implement IList, ICollection and IEnumerable? (4 个答案) 关闭 7 年前。 为什么ILis
我正在使用用 C# 创建的库。我一直致力于将一些代码移植到 F#,但必须使用 C# 库中的相当多的基础类型。 一段代码需要计算一个值列表并将其分配给类中的公共(public)字段/属性。该字段是一个包
此代码不起作用,但是: public virtual ICollection items { get { return (ICollection)items.Where(e => e.isVisibl
这个问题在这里已经有了答案: C# - sorting by a property (3 个答案) how to sort a collection by datetime in c# (4 个答案
这很奇怪,我试图在我的构造函数中使用 List 初始化我的 ICollection 并且发生了这种情况: Schedules = new List(); //OK CateringItems = ne
我想遍历设备上安装的所有语音。 在 TextSoSpeech 元数据中,我看到有 namespace Android.Speech.Tts { public class TextToSpeec
我有以下代码。为什么它总是采用“take(ICollection a)”方法?我认为它自己的对象应该是 LinkedList 或 HashSet,所以它应该调用另外两个 take 方法。 class
我的应用程序客户端/服务器有问题。我使用 MVVM 模式。在我看来,我有一个 DataGrid,它在我的 ViewModel 中与一个 ICollection 绑定(bind),代码行如下: publ
我是一名优秀的程序员,十分优秀!