[英]Linq query on many to many relationship
I work on a website for an organisation, this is non profit and I do it for free, so I am not looking for someone to do my job for me here, just a bit of help with something I am finding extremely tricky as it is relatively new to me 我在一个组织的网站上工作,这是非盈利性的,而且我是免费的,所以我并不是在这里找人来替我工作,我只是在寻找一些非常棘手的东西方面有所帮助对我来说比较新
Database which is using the aspnet entity stuff 使用aspnet实体资料的资料库
So the tables I am querying are 所以我要查询的表是
- AspNetRoles
- AspNetUserRoles
- AspNetUsers
I also have tables of my own 我也有自己的桌子
- myEvents (Id, Title, PrivateEvent)
- myEventsInRoles (EventId, RoleId)
- EventId is FK to myEvents(Id) and RoleId is FK to AspNetRoles(Id)
Now when I created my ADO Entity Model, because myEventsInRoles (like AspNetUserRoles) has only 2 columns which are both foreign keys, like AspNetUserRoles it does not generate an entity for it. 现在,当我创建ADO实体模型时,由于myEventsInRoles(如AspNetUserRoles)只有两列都是外键,如AspNetUserRoles,因此它不会为其生成实体。 So those two tables cannot be queried with linq.
因此无法使用linq查询这两个表。
So my scenario is this 所以我的情况是这样
A user logs in and wants to view the events list. 用户登录并想查看事件列表。 So example could be
所以例子可能是
- AspNetUsers
- Id=1
- AspNetUserRoles
- UserId=1, RoleId=1
- AspNetRoles
- Id=1, Name=Red Team
- Id=2, Name=Blue Team
- Id=3, Name=Green Team
- myEvents
- Id=1, Title=Big event, PrivateEvent=0
- Id=2, Title=Red And Green Team event, PrivateEvent=1
- Id=3, Title=Blue Team event, PrivateEvent=1
- myEventsInRoles
- EventId=2, RoleId=1
- EventId=2, RoleId=3
- EventId=3, RoleId=2
So let's say our user is in the red team 假设我们的用户在红色团队中
The events selected for them to view should be events 1 and 2 because they should fall under the following rules 选择供他们查看的事件应为事件1和2,因为它们应符合以下规则
So I would expect to see the Big event and the Red Team event 所以我希望看到大型活动和红队活动
I believe this is a bit more complicated but not dissimilar a query to another I also cannot workout, which is to simply get all Role Id's and Role Names for a User. 我认为这有点复杂,但与另一个我也无法锻炼的查询没有不同,它只是获取用户的所有角色ID和角色名称。
So after a flash of inspiration I managed to actually work out my two issues, the get roles for user turned out to be easier than i thought 因此,在获得灵感之后,我设法解决了两个问题,事实证明,为用户获取角色比我想象的要容易
var ctx = new ApplicationDbContext();
var user = HttpContext.Current.User;
var userId = user.Identity.GetUserId();
var roles =
ctx.Roles.Where(a => a.Users.Select(b => b.UserId).ToList().Contains(userId))
.ToList();
And my events issue 而我的事件
var events =
db.myEvents.Where(
e =>
e.date >= DateTime.Now
&& ((!e.@private)
|| (e.@private
&& e.AspNetRoles.Where(a => a.AspNetUsers.Select(b => b.Id)
.ToList()
.Contains(userId))
.Select(c => c.myEvents.Select(d => d.Id)
.ToList()
.Contains(e.Id))
.Any())))
.Select(
e =>
new EventItem
{
id = e.Id,
date = e.date,
description = e.description,
privateE = e.@private,
title = e.title,
type = e.type,
url = e.url
});
public class EventItem
{
public int id { get; set; }
public DateTime? date { get; set; }
public string description { get; set; }
public string title { get; set; }
public string type { get; set; }
public string url { get; set; }
public bool privateE { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.