locked
How to check count files inside Res is equal 1 or not ? RRS feed

  • Question

  • 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 Int license .

    so by any way  I need to check on result variable if returned one file or bigger than 1 

    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;
            }

    Monday, June 8, 2020 12:46 AM

Answers

  • Hi,

    cheong00 is right.

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

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 8, 2020 9:44 AM

All replies

  • I think you should use res.Count() instead. C# is case sensitive so the capital letter "C" matters, and since it is extension method you need to add "()" behind it.
    Monday, June 8, 2020 1:47 AM
    Answerer
  • Hi,

    cheong00 is right.

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

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 8, 2020 9:44 AM