Count number of rows in Linq query result using C# and VB.Net in Console Application

Last Reply 29 days ago By dharmendr

Posted 29 days ago

I work on c# app I face Issue I need to check count of files inside result equal 1 or not ?

when I use count res.count count not display as Intelicense.

so by any way I need to check on result variable if have one file or more. 

so How to do that Please ? 

        static void Main(string[] args)
        {
            DataTable dataTable = GetData();

            var res = from row in dataTable.AsEnumerable()
                      group row by row["Files"] into g
                      select g;

           if(res.count ==1 )
          {
          }
          else
          {
          }
        }
     
        public static DataTable GetData() 
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("PartId", typeof(int));
            dataTable.Columns.Add("Company", typeof(string));
            dataTable.Columns.Add("Files", typeof(string));
            dataTable.Columns.Add("Tab", typeof(string));
            dataTable.Columns.Add("Module", typeof(int));

            dataTable.Rows.Add(1222,"micro","Abc","source",1);
            dataTable.Rows.Add(1321, "silicon", "Abc", "Types", 3);
            dataTable.Rows.Add(1444, "cd2", "AutoD", "Rev", 10);
            dataTable.Rows.Add(1321, "cd3", "AutoD", "source", 11);
            dataTable.Rows.Add(1541,"mtvscro", "AutoD", "Rev", 12);
            dataTable.Rows.Add(9811, "tvs2", "Mog", "Dal", 6);
            dataTable.Rows.Add(1901, "tvs3", "Mog", "Mondo", 6);
            dataTable.Rows.Add(2111, "toyo", "Mog", "Pingo", 7);

            return dataTable;
        }

 

You are viewing reply posted by: dharmendr 29 days ago.
Posted 29 days ago

Hi ahmedsa,

Convert the result to List then you can use Count function.

Refer below sample code.

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

static void Main(string[] args)
{
    DataTable dataTable = GetData();

    var res = from row in dataTable.AsEnumerable()
                group row by row["Files"] into g
                select g;

    if (res.ToList().Count == 1)
    {
        // Do your Task.
    }
    else
    {
        // Do your Task.
    }
}

public static DataTable GetData()
{
    DataTable dataTable = new DataTable();
    dataTable.Columns.Add("PartId", typeof(int));
    dataTable.Columns.Add("Company", typeof(string));
    dataTable.Columns.Add("Files", typeof(string));
    dataTable.Columns.Add("Tab", typeof(string));
    dataTable.Columns.Add("Module", typeof(int));
    dataTable.Rows.Add(1222, "micro", "Abc", "source", 1);
    dataTable.Rows.Add(1321, "silicon", "Abc", "Types", 3);
    dataTable.Rows.Add(1444, "cd2", "AutoD", "Rev", 10);
    dataTable.Rows.Add(1321, "cd3", "AutoD", "source", 11);
    dataTable.Rows.Add(1541, "mtvscro", "AutoD", "Rev", 12);
    dataTable.Rows.Add(9811, "tvs2", "Mog", "Dal", 6);
    dataTable.Rows.Add(1901, "tvs3", "Mog", "Mondo", 6);
    dataTable.Rows.Add(2111, "toyo", "Mog", "Pingo", 7);

    return dataTable;
}

VB.Net

Sub Main()
    Dim dataTable As DataTable = GetData()
    Dim res = From row In dataTable.AsEnumerable()
                Group row By key = row("Files") Into Group
                Select Group
    If res.ToList().Count = 1 Then
        ' Do your Task.
    Else
        ' Do your Task.
    End If
End Sub
Public Function GetData() As DataTable
    Dim dataTable As DataTable = New DataTable()
    dataTable.Columns.Add("PartId", GetType(Integer))
    dataTable.Columns.Add("Company", GetType(String))
    dataTable.Columns.Add("Files", GetType(String))
    dataTable.Columns.Add("Tab", GetType(String))
    dataTable.Columns.Add("Module", GetType(Integer))
    dataTable.Rows.Add(1222, "micro", "Abc", "source", 1)
    dataTable.Rows.Add(1321, "silicon", "Abc", "Types", 3)
    dataTable.Rows.Add(1444, "cd2", "AutoD", "Rev", 10)
    dataTable.Rows.Add(1321, "cd3", "AutoD", "source", 11)
    dataTable.Rows.Add(1541, "mtvscro", "AutoD", "Rev", 12)
    dataTable.Rows.Add(9811, "tvs2", "Mog", "Dal", 6)
    dataTable.Rows.Add(1901, "tvs3", "Mog", "Mondo", 6)
    dataTable.Rows.Add(2111, "toyo", "Mog", "Pingo", 7)
    Return dataTable
End Function