开发者

Count Rows in Linq

开发者 https://www.devze.com 2023-04-12 12:21 出处:网络
I am very new in LINQ I have written following query : var duplicate = fromloginId in DataWorkspace.v2oneboxData.me_employees

I am very new in LINQ

I have written following query :

var duplicate =
    from  loginId in DataWorkspace.v2oneboxData.me_employees
    where loginId.me_login_name == this.me_login_name
              && loginId.me_pkey != this.me_pkey
    select loginId;

I want to count the rows returned in the result duplicate

I searched many of articles that says use duplicate.Count(). but i dont see count() in my intelisense

how do i count开发者_C百科 from result


How about doing it using extension methods:

 var count = me_employees.Where(me => me.me_login_name == this.me_login_name && me.me_pkey != this.me_pkey).Count();

Even better:

var count = me_employees.Count(me => me.me_login_name == this.me_login_name && me.me_pkey != this.me_pkey);

BIG NOTE: Ensure that you have referenced System.Core. System.Data.Linq as well but I assume you already referenced it.


You need the following line at the top of your file:

using System.Linq;

But there should be a little helper icon that appears over the missing reference on Count that has Visual Studio add this line automatically.

Update: This is a complete compilable example:

using System.Linq;

namespace ConsoleApplication11    {
    class Program
    {
        string me_login_name;
        int me_pkey;

        public static void Main()
        {
            new Program().Run();
        }

        private void Run()
        {
            IQueryable<v2oneboxDataEntity> me_employees = null;

            var duplicate =
                from  loginId in me_employees
                where loginId.me_login_name == this.me_login_name
                        && loginId.me_pkey != this.me_pkey
                select loginId;

            var count = duplicate.Count();            
        }

        // Define other methods and classes here
        class v2oneboxDataEntity 
        {
            public string me_login_name { get; set; }
            public int me_pkey { get; set; }
        }
    }
}

What version of Visual Studio are you using? And what is your target version of the .NET framework? (Requires >= VS 2008, and >= .NET 3.5.)


If you're using lambda expressions then make sure you have referenced System.Linq.Expressions namespace.

var cardCount = datatable
    .AsEnumerable()
    .Where(p => p.Field<decimal>("SpotID") ==
        Convert.ToDecimal(currentActivatedSpot))
    .Count();
0

精彩评论

暂无评论...
验证码 换一张
取 消