Saturday 13 July 2013

Use of long count in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Use of long count in LINQ

Long count is similar to count as it works same but the only difference between them is that long count returns a 64 bit integer.

For Example:-

int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

      Int64 res = marks.LongCount(x => x % 2 == 0);
                        OR
long res_long = marks.LongCount(x => x % 2 == 0);
       Response.Write(res_long);

The output of this code will be 5.


Similarly we can apply this on generics, and database too.

On Generics:-

  List<data> dt = new List<data>()
        {
            new data{roll_no=1, student="isha", per=100},
            new data{roll_no=2, student="sneha", per=89},
            new data{roll_no=3, student="rahul", per=34},
            new data{roll_no=4, student="renu", per=34},
            new data{roll_no=5, student="sapna", per=89}
        };

        Int64 count_res = dt.LongCount(x => x.per > 50);

On Database:-

  Int64 res_count = dc.GetTable<Class1>().LongCount(x => x.id > 8);

Kindly read Hour 5 to article to get the implementation of database and use of get table.


No comments:

Post a Comment