如何将此循环代码转换为单个LINQ实现?

时间:2018-06-14 02:49:09

标签: c# linq

我正在尝试优化下面的代码,逐个循环遍历对象并进行数据库查找。我想创建一个LINQ语句,它将在一个事务中执行相同的任务。

这是我效率低下的循环代码;

IStoreUnitOfWork uow = StoreRepository.UnitOfWorkSource.GetUnitOfWorkFactory().CreateUnitOfWork(); 
var localRunners = new List<Runners>();
foreach(var remoteRunner in m.Runners) {                                                           
    var localRunner = uow.CacheMarketRunners.Where(x => x.SelectionId == remoteRunner.SelectionId && x.MarketId == m.MarketId).FirstOrDefault();  
    localRunners.Add(localRunner);
}

这是我在单个查询中尝试做同样事情的非常可行的尝试。嗯,这甚至​​不是一次尝试。我不知道从哪里开始。 remoteRunners对象具有复合键。

IStoreUnitOfWork uow = StoreRepository.UnitOfWorkSource.GetUnitOfWorkFactory().CreateUnitOfWork(); 
var localRunners = new List<Runners>();
var localRunners = uow.CacheMarketRunners.Where(x =>
   x.SelectionId in remoteRunners.SelectionId &&
   x.MarketId    in remoteRunners.MarketId);

感谢您寻找

2 个答案:

答案 0 :(得分:2)

所以你有一个对象m,它有一个属性MarketId。对象m也有一系列Runners,其中每个Runner都有一个属性SelectionId

您的数据库有CacheMarketRunners。每个CacheMarketRunner都有MarketIdSelectionId

您的查询应返回CacheMarketRunners所有MarketId等于m.MarketIdSelectionId m.Runners.SelectionId的所有m

如果您的Runners没有太多var requestedSelectionIds = m.Runners.Select(runner => runner.SelectionId); var result = CacheMarketRunners.Where(cacheMarketRunner => cacheMarketRunner.MarketId == m.MarketId && requestedSelectionIds.Contains(cacheMarketRunner.SelectionId)); ,少于250,请考虑使用Queryable.Contains

//td[@id='cell_saturday1' and @name='06/02/2018' and contains(@onclick,'onClick(this);')]

答案 1 :(得分:0)

要提高性能,您需要缓存交易结果:

var marketRunners = uow.CacheMarketRunners.Where(x => x.MarketId == m.MarketId).ToList();

有关uow的交易结果存储在List中,因此您在for循环中没有交易。因此应该提高绩效:

var localRunners = new List<Runners>();
foreach(var remoteRunner in m.Runners) {                                                           
    var localRunner = marketRunners.FirstOrDefault(x => x.SelectionId == remoteRunner.SelectionId);
    localRunners.Add(localRunner);
}

您甚至可以删除for循环:

var localRunners = m.Runners.Select(remoteRunner => marketRunners.FirstOrDefault(x => x.SelectionId == remoteRunner.SelectionId)).ToList();