codememo

C#에서 목록이 비어 있는지 확인합니다.

tipmemo 2023. 4. 13. 20:53
반응형

C#에서 목록이 비어 있는지 확인합니다.

범용 목록 개체가 있습니다.목록이 비어 있는지 확인해야 합니다.

어떻게 하면List<T>C#은 비어있습니까?

다음을 사용할 수 있습니다.

bool isEmpty = !list.Any();
if(isEmpty)
{
    // ...
}  

리스트가 다음과 같은 경우null다음을 사용할 수 있습니다.

bool isNullOrEmpty = list?.Any() != true;

사용하고 있는 리스트의 실장되어 있는 경우는,IEnumerable<T>또한 Linq는 옵션입니다.Any:

if (!list.Any()) {

}

그렇지 않으면 일반적으로Length또는Count어레이의 속성 및 수집 유형의 속성.

    If (list.Count==0){
      //you can show your error messages here
    } else {
      //here comes your datagridview databind 
    }

데이터 그리드를 false로 표시하고 다른 섹션에 표시할 수 있습니다.

를 사용하는 것은 어떨까요?Count소유물.

 if(listOfObjects.Count != 0)
 {
     ShowGrid();
     HideError();
 }
 else
 {
     HideGrid();
     ShowError();
 }

심플을 사용하세요.IF진술

List<String> data = GetData();

if (data.Count == 0)
    throw new Exception("Data Empty!");

PopulateGrid();
ShowGrid();
var dataSource = lst!=null && lst.Any() ? lst : null;
// bind dataSource to gird source

gridview 자체에는 바인드하고 있는 데이터 소스가 비어 있는지 체크하는 메서드가 있어 다른 것을 표시할 수 있습니다.

그리드 뷰를 사용하는 경우 빈 데이터 템플릿을 사용합니다.http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        runat="server">

        <emptydatarowstyle backcolor="LightBlue"
          forecolor="Red"/>

        <emptydatatemplate>

          <asp:image id="NoDataImage"
            imageurl="~/images/Image.jpg"
            alternatetext="No Image" 
            runat="server"/>

            No Data Found.  

        </emptydatatemplate> 

      </asp:gridview>

언급URL : https://stackoverflow.com/questions/18867180/check-if-list-is-empty-in-c-sharp

반응형