Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# EF Linq bitwise question

Ok for example, I am using bitwise like such: Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8 etc...

I am using an Entity Framework class of Business.

I am using a class and passing in a value like 7 (Monday, Tuesday, Wednesday).

I want to return records that match any of those days

    public List<Business> GetBusinesses(long daysOfWeek)
    {
         using (var c = Context())
         {
              return c.Businesses.Where(???).ToList();
         }
    }

Any help would be appreciated. Thanks!

EDIT

Ok, so I am attempting the following:

var b = new List<Business>();
var b1 = new Business(){DaysOfWeek = 3};
b.Add(b1);
var b2 = new Business() { DaysOfWeek = 2 };
b.Add(b2);
var decomposedList = new[]{1};
var l = b.Where(o => decomposedList.Any(day => day == o.DaysOfWeek)).ToList(); 

But l returns 0 results assuming in the decomposedList(1) I am looking for monday. I created b1 to contain Monday and Tuesday.

like image 612
Cyberdrew Avatar asked Jan 14 '11 21:01

Cyberdrew


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Use the bitwise and operator & to combine your desired flags with the actual flags in the database and then check for a non-zero result.

        var b1 = new { DaysOfWeek = 3 };
        var b2 = new { DaysOfWeek = 2 };
        var b = new[] { b1, b2 };
        var filter = 1;

        var l = b.Where(o => (filter & o.DaysOfWeek) != 0);
        foreach (var x in l)
        {
            Console.WriteLine(x);
        }

If you have an array of filter values simply combined then with an OR | first:

        var filterArray = new []{1, 4};
        var filter = filterArray.Aggregate((x, y) => x | y);
like image 137
Ian Mercer Avatar answered Sep 29 '22 11:09

Ian Mercer