Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按相关数据过滤linq/实体查询结果_C#_Asp.net Mvc_Entity Framework_Linq - Fatal编程技术网

C# 按相关数据过滤linq/实体查询结果

C# 按相关数据过滤linq/实体查询结果,c#,asp.net-mvc,entity-framework,linq,C#,Asp.net Mvc,Entity Framework,Linq,我使用的是MVC5 EF6和Identity 2.1 我有两门课: public class Incident { public int IncidentId {get; set;} ...//Title, Description, etc public virtual ICollection<FollowedIncident> FollowedIncidents { get; set; }

我使用的是MVC5 EF6和Identity 2.1

我有两门课:

    public class Incident 
        {
           public int IncidentId {get; set;}
           ...//Title, Description, etc
           public virtual ICollection<FollowedIncident> FollowedIncidents { get; set; }
           public virtual ApplicationUser User { get; set; }
        }

        public class FollowedIncident
        {
           public int FollowedIncidentId { get; set; }
           public string UserId { get; set; }
           public int IncidentId { get; set; }

           public virtual Incident Incident { get; set; }
           public virtual ApplicationUser User { get; set; }
        }
这(没有错误,但根本不过滤):

对我来说,似乎应该是这么简单:

listUnpaged = listUnpaged.Where(s => s.FollowedIncidents.UserId == userId));
但是,linq扩展似乎不喜欢相关的数据子属性?(我为我的编程术语道歉,因为我还没有完全拼凑出所有的名称。)

有人知道如何做到这一点吗?看来我可能连想都没想对吧?(…因为在过去,我总是使用相关数据来补充或向结果添加属性。这将是我第一次希望通过相关数据缩小结果范围。)


谢谢。

事实上,你把事情弄错了。。由于事件是FollowedIncident的导航属性,所以您只需使用

IQueryable<Incident> listUnpaged = db.FollowedIncidents
                                     .Where(a => a.UserId == userid)
                                     .Select(a => a.Incident)
                                     .OrderByDescending(d => d.IncidentDate);
这就像说

Select * 
From Incidents
Where Id IN (Select IncidentId
             From FollowedIncident 
             Where UserId = @UserId)

您缺少的是
Any
(而不是
All
Where
):
listunpage=listunpage.Where(s=>s.followdincidents.Any(t=>t.UserId==UserId))@没希望成功!!谢谢我读到了
Any
All
之间的差异,并得出结论,没有任何差异,但这可能更多地是出于与性能相关的立场?我会继续读下去。无论如何,这就是为什么我不赌博(用实际的钱)。。。生活和学习。谢谢
FollowedIncidents
是集合的导航属性,您怎么能说它与此
s.FollowedIncidents.UserId一起工作良好呢?我本来打算使用Any(),但由于某些原因它看起来不太正确。。我猜也是一样,这需要更多地了解OP的模型,比如为什么
事件
后续事件
相关,事实上,在这种情况下,他想要的是
事件
而不是
后续事件
。。。使用
Any
也是我的猜测。现在离回家检查生成的查询的时间太近了,我无法查看它们是什么样子的,但是看起来像
。其中(a=>a.UserId==UserId)
将创建一个Where=子句,而
Any(a=>a.UserId==UserId)
将创建一个IN或子连接或其他东西。。我会加上它,他们可以决定:)@JamieD77谢谢你的回复和回答,我想从这个方向(你的第二次编辑,第一个例子)开始,但一切都是围绕着这个事件。因此,在查询的后面,我将筛选属于事件类的
isbanked
IsDeleted
等。看来任何人都会成功。谢谢
listUnpaged = listUnpaged.Where(s => s.FollowedIncidents.UserId == userId));
IQueryable<Incident> listUnpaged = db.FollowedIncidents
                                     .Where(a => a.UserId == userid)
                                     .Select(a => a.Incident)
                                     .OrderByDescending(d => d.IncidentDate);
IQueryable<Incident> listUnpaged = db.Incidents
                                 .Where(a => a.FollowedIncidents.Any(b => b.UserId == userid)
                                 .OrderByDescending(d => d.IncidentDate);
Select * 
From Incidents
Where Id IN (Select IncidentId
             From FollowedIncident 
             Where UserId = @UserId)