Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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
列表上的C#条件语句_C#_Asp.net_Asp.net Mvc 3_List - Fatal编程技术网

列表上的C#条件语句

列表上的C#条件语句,c#,asp.net,asp.net-mvc-3,list,C#,Asp.net,Asp.net Mvc 3,List,条件:如果假设我的列表包含n计数。然后,如果任何索引的任何“本机元素”持有某些值,则显示其他字段,反之亦然 i、 e。 像这样的 public ActionResult actionName() { AbcViewModel viewModel=new AbcViewModel(); viewModel=model.getAbcList(); return View(viewModel); } @if (Model.languageList.Any(x => x

条件:如果假设我的列表包含
n
计数。然后,如果任何索引的任何“本机元素”持有某些值,则显示其他字段,反之亦然

i、 e。 像这样的

public ActionResult actionName() 
{
    AbcViewModel viewModel=new AbcViewModel();
    viewModel=model.getAbcList();
    return View(viewModel);
}
@if (Model.languageList.Any(x => x.nativeLanguage.IsNotEmpty() == false)
{ 
    @LabelFor(x=>x.other) 
}
else
{
    @LabelFor(x=>x.native)
}
@if (Model.languageList.Any(x => string.IsNullOrEmpty(x.nativeLanguage))
{ 
    @LabelFor(x => x.other) 
}
else
{
    @LabelFor(x => x.native)
}
我认为这不是正确的语法(IsNotEmpty不是有效的方法)


告诉我正确的方法。

你可以这样做

public ActionResult actionName() 
{
    AbcViewModel viewModel=new AbcViewModel();
    viewModel=model.getAbcList();
    return View(viewModel);
}
@if (Model.languageList.Any(x => x.nativeLanguage.IsNotEmpty() == false)
{ 
    @LabelFor(x=>x.other) 
}
else
{
    @LabelFor(x=>x.native)
}
@if (Model.languageList.Any(x => string.IsNullOrEmpty(x.nativeLanguage))
{ 
    @LabelFor(x => x.other) 
}
else
{
    @LabelFor(x => x.native)
}
用于检查指定的字符串是空字符串还是空字符串

试试这个:

@if (Model.languageList.Any(x=> !string.IsNullOrEmpty(x.nativeLanguage))==false){ @LabelFor(x=>x.other) }
    else{@LabelFor(x=>x.native)}
试试这个:

@if (!Model.languageList.Any(x=> string.IsNullOrEmpty(x.nativeLanguage))
{ 
  @LabelFor(x=>x.other) 
 }
 else 
 {
   @LabelFor(x=>x.native)
  }

如果列表中充满了字符串,那么应该能够调用如下方法

public ActionResult actionName() 
{
    AbcViewModel viewModel=new AbcViewModel();
    viewModel=model.getAbcList();
    return View(viewModel);
}
@if (Model.languageList.Any(x => x.nativeLanguage.IsNotEmpty() == false)
{ 
    @LabelFor(x=>x.other) 
}
else
{
    @LabelFor(x=>x.native)
}
@if (Model.languageList.Any(x => string.IsNullOrEmpty(x.nativeLanguage))
{ 
    @LabelFor(x => x.other) 
}
else
{
    @LabelFor(x => x.native)
}
如果您还有其他数据类型,您仍然可以为自己编写一个方法来完成相同的工作