我有一个查询需要通过循环遍历主对象的值来检查对象列表中的值。怎么可以在下面重新编码?
foreach (var kd in dto.KeyDriverModels)
{
var keyDriverModelNodeValue = result.SingleOrDefault(x => x.KeyDriverModelId == kd.ID);
kd.SelectionStatus = keyDriverModelNodeValue != null ?
keyDriverModelNodeValue.SelectionStatus : string.Empty;
}
答案 0 :(得分:0)
我尝试为您的代码建模并将代码重写为Linq。看看是否有效
static DTO dto = new DTO();
static void Main(string[] args)
{
List<KeyDriver> keyDrivers = new List<KeyDriver>();
List<KeyDriver> result = keyDrivers.Where(x => x.KeyDriverModelId > 1000).ToList();
foreach (var kd in dto.KeyDriverModels)
{
var keyDriverModelNodeValue = result.SingleOrDefault(x => x.KeyDriverModelId == kd.ID);
kd.SelectionStatus = keyDriverModelNodeValue != null ?
keyDriverModelNodeValue.SelectionStatus : string.Empty;
}
//new code
var kd1 = from r in result
join d in dto.KeyDriverModels
on r.KeyDriverModelId equals d.ID into drs
from dr in drs
select new { dr }.dr.SelectionStatus != null ? dr.SelectionStatus : string.Empty;
}
}
public class DTO
{
public List<KeyDriverModel> KeyDriverModels { get; set; }
}
public class KeyDriverModel
{
public int ID { get; set; }
public string SelectionStatus { get; set; }
}
public class KeyDriver
{
public int KeyDriverModelId { get; set; }
public string SelectionStatus { get; set; }
}
&#13;