3

I want to get this query at last:

 select * from tableName where columnName & 2 = 2 and columnName & 4 = 4

How can I use LINQ to generate this script?

5
  • 2
    @Henk, in SQL you don't use double ==. See SQL WHERE caluse information here and the operator section: w3schools.com/sql/sql_where.asp Commented Jul 5, 2011 at 11:13
  • @flip: But I think this is C# (LINQ), it pays to be clear about that. Commented Jul 5, 2011 at 11:18
  • @Henk, you don't do select * from in LINQ. It's from x in. Commented Jul 5, 2011 at 11:19
  • Right, I missed the *. Does SQL do bitwise-AND ? Commented Jul 5, 2011 at 11:21
  • Yes, bitwise AND (&), OR(|), XOR (^). Commented Jul 5, 2011 at 11:36

2 Answers 2

11

You can do bitwise operations in C# ( and in LINQ queries ) with either & or | depending on what bitwise operation you want.

var query =
            from row in context.tableName
            where (row.columnName & 2) == 2 && (row.columnName & 4) == 4
            select row
Sign up to request clarification or add additional context in comments.

2 Comments

Will this run on the app server or the DB server, though?
@Justin, LINQ is evaluated to SQL in the end, so AFAIK it will run on the SQL Server. As long as you dont have context.tableName.ToList() instead, which would make it LINQ to Object instead of LINQ to EF / SQL.
2
var query = from r in context.tableName 
    where r.columnName & 2 == 2 and r.columnName & 4 == 4
    select r;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.