Linq Select with Or Statement VS Separate Queries

时间:2018-05-21 11:57:36

标签: c# entity-framework linq

我有30个表,我想扫描所有表中的所有数据但是我在使用Entity Framework的MVC项目中遇到超时错误。您可以想象alphabeth中每个字母的扫描数据就像这段代码一样;

               foreach (var item in db.A_Player.Where(i=>i.CurrentTeamId == teamIdEv || i.CurrentTeamId == teamIdDep))
                {
                    PlayerEntity player = new PlayerEntity()
                    {
                        Name = item.Name,
                        Surname = item.Surname,
                        Id = item.Id,
                        CurrentTeamId = item.CurrentTeamId

                    };

                    if (item.CurrentTeamId == teamIdEv)
                    {
                        playerListEv.Add(player);
                    }
                    else
                    {
                        playerListDep.Add(player);
                    }

                }
                foreach (var item in db.B_Player.Where(i => i.CurrentTeamId == teamIdEv || i.CurrentTeamId == teamIdDep))
                {
                    PlayerEntity player = new PlayerEntity()
                    {
                        Name = item.Name,
                        Surname = item.Surname,
                        Id = item.Id,
                        CurrentTeamId = item.CurrentTeamId

                    };

                    if (item.CurrentTeamId == teamIdEv)
                    {
                        playerListEv.Add(player);
                    }
                    else
                    {
                        playerListDep.Add(player);
                    }

                }

解决问题的问题我可以使用单独的查询,如下面的代码或两者都相同?这两者都给出与速度相同的结果?如何超出超时错误?

foreach (var item in db.B_Player.Where(i => i.CurrentTeamId == teamIdEv))
                {
                    PlayerEntity player = new PlayerEntity()
                    {
                        Name = item.Name,
                        Surname = item.Surname,
                        Id = item.Id,
                        CurrentTeamId = item.CurrentTeamId

                    };

                    if (item.CurrentTeamId == teamIdEv)
                    {
                        playerListEv.Add(player);
                    }
                    else
                    {
                        playerListDep.Add(player);
                    }

                }

                foreach (var item in db.B_Player.Where(i => i.CurrentTeamId == teamIdDep))
                {
                    PlayerEntity player = new PlayerEntity()
                    {
                        Name = item.Name,
                        Surname = item.Surname,
                        Id = item.Id,
                        CurrentTeamId = item.CurrentTeamId

                    };

                    if (item.CurrentTeamId == teamIdEv)
                    {
                        playerListEv.Add(player);
                    }
                    else
                    {
                        playerListDep.Add(player);
                    }

                }

2 个答案:

答案 0 :(得分:1)

db.B_Player DbSetPlayerEntities吗?

如果是这样,则不需要foreach。你可以这样做:

这只是伪代码我现在没有IDE可供使用。

playerListEv.AddRange(db.B_Player.Where(i => i.CurrentTeamId == teamIdEv).ToList());
playerListDep.AddRange(db.B_Player.Where(i => i.CurrentTeamId == teamIdDep).ToList());

答案 1 :(得分:1)

首先,通过最小化从表中拉回的列(如果有很多列),投影可以在这方面提供帮助。

其次,你可以做两个较小的查询

var evs = db.B_Player.Where(i => i.CurrentTeamId == teamIdEv)
                     .Select(x => new PlayerEntity // projection
                     {
                        Name = item.Name,
                        Surname = item.Surname,
                        Id = item.Id,
                        CurrentTeamId = item.CurrentTeamId
                     }).ToList();

var deps= db.B_Player.Where(i => i.CurrentTeamId != teamIdEv)
                     .Select(x => new PlayerEntity // projection
                     {
                        Name = item.Name,
                        Surname = item.Surname,
                        Id = item.Id,
                        CurrentTeamId = item.CurrentTeamId
                     }).ToList();

或者你可以投射所有玩家并在内存中过滤它们,

但是,你需要按照这里的瓶颈来工作。

  • CurrentTeamId是否适当index
  • 您的数据库Normalized是否正确(正如您所说,您有30张桌子)