Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
.net LINQ到SQL:如何创建条件包含之类的东西?_.net_Linq_Entity Framework_Include_Conditional Statements - Fatal编程技术网

.net LINQ到SQL:如何创建条件包含之类的东西?

.net LINQ到SQL:如何创建条件包含之类的东西?,.net,linq,entity-framework,include,conditional-statements,.net,Linq,Entity Framework,Include,Conditional Statements,我想创建一个查询,其中包含其实体具有类型==1的事件的用户。我还希望将事件条目加载到EventsCollection中。我的代码类似于.Includeu=>u.EventsCollection,但这只加载所有的事件条目——我只希望加载类型==1的条目 我有这样简单的模型: public class User { public int UserId { get; set; } public ICollection<Event> EventsCollection { g

我想创建一个查询,其中包含其实体具有类型==1的事件的用户。我还希望将事件条目加载到EventsCollection中。我的代码类似于.Includeu=>u.EventsCollection,但这只加载所有的事件条目——我只希望加载类型==1的条目

我有这样简单的模型:

public class User
{
    public int UserId { get; set; }

    public ICollection<Event> EventsCollection { get; set; }
}
public class Event
{
    public int EventId { get; set; }
    public int UserId { get; set; }
    public int Type { get; set; }

    public User User {get; set; }
}
var filteredEventsCollection = db.Events
    .Where(ev => ev.Type == 1)
    .ToList(); //Materialize query here
var usersWithFilteredEvents = db.Users
    .Where(u => u.EventsCollection.Any(ev => ev.Type == 1))
    .ToList();
现在我们应该像这样查询用户:

public class User
{
    public int UserId { get; set; }

    public ICollection<Event> EventsCollection { get; set; }
}
public class Event
{
    public int EventId { get; set; }
    public int UserId { get; set; }
    public int Type { get; set; }

    public User User {get; set; }
}
var filteredEventsCollection = db.Events
    .Where(ev => ev.Type == 1)
    .ToList(); //Materialize query here
var usersWithFilteredEvents = db.Users
    .Where(u => u.EventsCollection.Any(ev => ev.Type == 1))
    .ToList();

就这些!EF将自己用第一个查询中的数据替换EventsCollection。

如果您仅尝试获取userID==1的事件,则可以使用以下方法。这将仅为用户提供事件集合中所需的事件

        User u = new User()
        {
            UserId = 60
        };

        Event e1 = new Event() { EventId = 1, UserId = 2, Type = 3, User = u };
        Event e2 = new Event() { EventId = 2, UserId = 3, Type = 4, User = u };
        Event e3 = new Event() { EventId = 3, UserId = 4, Type = 5, User = u };
        Event e_Test = new Event() { EventId = 7, UserId = 1, Type = 5, User = u };

        u.EventsCollection = new List<Event>();

        u.EventsCollection.Add(e1);
        u.EventsCollection.Add(e2); 
        u.EventsCollection.Add(e3);
        u.EventsCollection.Add(e_Test);


        var h = from use in u.EventsCollection where use.UserId == 1 select use;

这将为您提供您想要的:

void Main(){

List<User> users = new List<User>{
 new User{ UserId=1},
 new User{ UserId=2}
};

List<Event> events = new List<Event>{
    new Event{ EventId=1, UserId=2, Type=0},
    new Event{ EventId=2, UserId=2, Type=1},
    new Event{ EventId=4, UserId=2, Type=1},
    new Event{ EventId=5, UserId=2, Type=0},
    new Event{ EventId=1, UserId=1, Type=1},
    new Event{ EventId=2, UserId=1, Type=0},
    new Event{ EventId=4, UserId=1, Type=0},
    new Event{ EventId=5, UserId=1, Type=1},
};



var result = users.GroupJoin(events.Where (e =>e.Type==1 ),
    u => u.UserId,
    e => e.UserId,
    (a, b) => new {a.UserId,b});    
//result.Dump();

}

 // Define other methods and classes here
public class User
{
  public int UserId { get; set; }

  public ICollection<Event> EventsCollection { get; set; }
}
public class Event
{
  public int EventId { get; set; }
  public int UserId { get; set; }
  public int Type { get; set; }

  public User User {get; set; }
}
您不能使用Include和filter来筛选急切加载或延迟加载的实体,但您可以使用投影来伪造它:

var result = (from u in db.Users
              where u.EventsCollection.Any(ev => ev.Type == 1)
              select new {
                  User = u,
                  Events = u.EventsCollection.Where(ev => ev.Type == 1),
              }).ToList()    // materialize query here!
             .Select(uev => uev.User)
             .ToList();
这将返回一个IEnumerable,其中包含您希望在EventCollection中显示的事件 如果以前已将其他事件实体加载到上下文中,则此操作将不起作用! 还必须为User.events集合禁用延迟加载,方法是删除已执行的虚拟集合,或为整个上下文禁用虚拟集合。 看见
无论如何,我可能会直接使用投影,因为这样更安全。

不太确定您要问什么,但可能您正在寻找与SQL等效的LINQ Contains,其中。。。在SELECT.LINQ Contains中,可以帮助我查找事件类型==1的用户实体,但我还想将它们加载到EventsCollection.load with lazy load?你什么时候可以进入酒店?顺便说一句,别忘了虚拟!是的,延迟加载应该会有所帮助,但对数据库的请求数量将增加,用户集合中的每个实体将再增加一个请求!我正在寻找在一个查询中加载所有内容的解决方案。无论如何,谢谢。这里有一个类似的问题,我需要查询用户实体,而不仅仅是事件。这并没有回答问题。他正在尝试获取事件类型==1的用户。另外,您的用户与事件集合没有关系。User=>UserID=60,在您的事件中,UserID为1到5。谢谢您的回答,但这个解决方案对我不起作用。似乎在.AsEnumerable调用上没有查询具体化。我所有的EventsCollection=null。但我发现了一个非常类似的解决方案。你可以在我的第一篇文章中查看它。@Alexander:好吧,在使用可枚举项之前,它实际上不会具体化,但也许你是对的,ToList肯定会这么做。我会改变的。