Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7730671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:17:01+00:00 2026-06-01T06:17:01+00:00

I am trying to use logical operators in the following lambda expression int count

  • 0

I am trying to use logical operators in the following lambda expression

int count = dataContext.Users.Count(u => u.ID == 1 && u.Name == "name");

below is the complete function

private void Login(int id, string password)
{
    MyDataContext dataContext = new MyDataContext();
    int count = dataContext.Users.Count(u => u.ID == 1 && u.Name == "name");
}

I’m using Microsoft SQL Server 2008 Express 2008 Edition and User table looks like

ID int autogenerated
Name string

but Visual Studio 2010 gives the following design time errors “Invalid expression terms”. Any idea what I might be doing wrong?

I get the following error messages

  • Error 2 ) expected …\FormAdd.cs 105 123
  • Error 3 ; expected …\FormAdd.cs 105 126
  • Error 5 ; expected …\FormAdd.cs 105 158
  • Error 7 ; expected …\FormAdd.cs 105 202
  • Error 4 Invalid expression term ‘)’ …\FormAdd.cs 105 158
  • Error 6 Invalid expression term ‘)’ …\FormAdd.cs 105 202
  • Error 1 Invalid expression term ‘=>’ …\FormAdd.cs 105 123

Intellisense doesn't show user object properties

List of Errors

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:17:02+00:00Added an answer on June 1, 2026 at 6:17 am

    The code you show in the screenshot is

    int count = dataContext.Users.Count(u => u.ID == 1 && u => u.Name == "name");
                                                            **
    

    This is illegal code, and the part with the ** underneath it is the source of the error.

    The code you show in the body of your question is

    int count = dataContext.Users.Count(u => u.ID == 1 && u.Name == "name");
    

    These are different. The first, is incorrect, as the IDE points out to you. The latter, is correct.

    The signature for the overload of Count you are using is

     int Count<T>(this IEnumerable<T> source, Predicate<T> predicate);
    

    This means to invoke it on dataContext.Users, you need to provide a Predicate<User>. A Predicate<User> is a function that maps User to bool. One way to write this is

     static bool UserPredicate(User u) {
         return u.ID == 1 && u.Name == "name";
     }
    

    and then you can say

    int count = dataContext.Users.Count(UserPredicate);
    

    Another way is to say

    int count = dataContext.Users.Count(
        delegate(User u) { return u.ID == 1 && u.Name == "name" }
    );
    

    This lets you define the predicate inline. It’s a nice feature. Note that the expression that effectively defines the predicate

    u.ID == 1 && u.Name == "name"
    

    is the same as in the case of UserPredicate defined above.

    And finally, Lambda expressions allow you to get rid of some of that unnecessary fluff.

    int count = dataContext.Users.Count(
        u => f(u)
    )
    

    Where f(u) is an expression in u. One expression that you can use is

    u.ID == 1 && u.Name == "name"
    

    Again, the same expression. See, your attempt in the IDE was illegal because you tried to write

    u.ID == 1 && u => u.Name == "name"
    

    and that is clearly not a legal expression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
How to I use logical operators to determine if a bit is set, or
I'm looking for help with the NOT (!) logical operator. I'm trying to use
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I am trying use a Java Uploader in a ROR app (for its ease
Hi I'm trying use a datepicker on a field I have. I'm trying to
Trying to use an excpetion class which could provide location reference for XML parsing,
Trying to use a guid as a resource id in a rest url but
Trying to use this method (gist of which is use self.method_name in the FunnyHelper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.