C# 列表中不存在项目时发生故障如何检查
我有一个linq查询,但是当没有设置性别来反对用户时,它就失败了C# 列表中不存在项目时发生故障如何检查,c#,linq,C#,Linq,我有一个linq查询,但是当没有设置性别来反对用户时,它就失败了 List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode(); var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode); if (codesForThisItem.Count() > 0 ) { if (code
List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode();
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Count() > 0 )
{
if (codesForThisItem.First(x => x.code == Constants.Sport) != null)
sport = codesForThisItem.First(x => x.code == Constants.Sport);
if(codesForThisItem.First(x => x.code == Constants.Gender) !=null)
gender = codesForThisItem.First(x => x.code == Constants.Gender);
}
但是它实际上是这个项目的代码失败了,我不能使用count来捆绑它,因为它可能会有其他代码对它不利。如果它不在列表中,我最好的捕获方法是用空字符串替换它。使用
First或default
而不是First
。第一个将在谓词不匹配任何元素时引发异常(序列不包含匹配的元素)
List<StandardLookUpList > _AnalsisCodes = GetAnayalsisCodesForExportCode();
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Any())
{
if (codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport) != null)
{
sport = codesForThisItem.First(x => x.code == Constants.Sport);
}
if (codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender) != null)
{
gender = codesForThisItem.First(x => x.code == Constants.Gender);
}
}
List_anasiscodes=getanayalsicodesforexportcode();
var codesforthitem=\u anasiscodes.Where(w=>w.ItemCode==item.ItemCode);
if(codesforthitem.Any())
{
if(codesforthitem.FirstOrDefault(x=>x.code==Constants.Sport)!=null)
{
sport=codesforthistem.First(x=>x.code==Constants.sport);
}
if(codesforthitem.FirstOrDefault(x=>x.code==Constants.Gender)!=null)
{
gender=codesforthistem.First(x=>x.code==Constants.gender);
}
}
使用First或default
而不是First
。第一个将在谓词不匹配任何元素时引发异常(序列不包含匹配的元素)
List<StandardLookUpList > _AnalsisCodes = GetAnayalsisCodesForExportCode();
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Any())
{
if (codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport) != null)
{
sport = codesForThisItem.First(x => x.code == Constants.Sport);
}
if (codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender) != null)
{
gender = codesForThisItem.First(x => x.code == Constants.Gender);
}
}
List_anasiscodes=getanayalsicodesforexportcode();
var codesforthitem=\u anasiscodes.Where(w=>w.ItemCode==item.ItemCode);
if(codesforthitem.Any())
{
if(codesforthitem.FirstOrDefault(x=>x.code==Constants.Sport)!=null)
{
sport=codesforthistem.First(x=>x.code==Constants.sport);
}
if(codesforthitem.FirstOrDefault(x=>x.code==Constants.Gender)!=null)
{
gender=codesforthistem.First(x=>x.code==Constants.gender);
}
}
您可以改为使用.FirstOrDefault()
,然后在继续之前检查结果是否为空。您所写内容的问题在于.First()
总是希望有匹配的结果:
List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode();
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Any())
{
var sportResult = codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport);
if (sportResult != null) sport = sportResult;
var genderResult = codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender);
if (genderResult != null) gender = genderResult;
}
您可以改为使用
.FirstOrDefault()
,然后在继续之前检查结果是否为null。您所写内容的问题在于.First()
总是希望有匹配的结果:
List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode();
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Any())
{
var sportResult = codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport);
if (sportResult != null) sport = sportResult;
var genderResult = codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender);
if (genderResult != null) gender = genderResult;
}
if
语句是无用的,除非我遗漏了什么。执行赋值时只需使用FirstOrDefault
。@MariusBughiu在这种情况下,sport
和gender
可能已经在代码的其他地方设置了值。OP只想在谓词匹配时更新它们。我完全同意,但这将是一个超出简单修复的额外更改。if
语句是无用的,除非我遗漏了什么。执行赋值时只需使用FirstOrDefault
。@MariusBughiu在这种情况下,sport
和gender
可能已经在代码的其他地方设置了值。OP只想在谓词匹配时更新它们。我完全同意,但这将是一个超出简单修复的额外更改。这是一个更好的答案-它重用谓词的结果。很好@斯科特,谢谢。从您的版本来看,我认为有可能运行同一个查询两次,这显然是没有效率的,除非EF足够聪明来缓存结果这是一个更好的答案-它重用谓词的结果。很好@斯科特,谢谢。从您的版本来看,我认为有可能运行同一个查询两次,这显然是没有效率的,除非EF足够聪明,能够缓存结果