C#EF Linq的按位问题 [英] C# EF Linq bitwise question

查看:175
本文介绍了C#EF Linq的按位问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,例如,我使用的按位像这样的:星期一= 1,星期二= 2,星期三= 4,星期四= 8等等...

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();
         }
    }

任何帮助将是AP preciated。谢谢!

Any help would be appreciated. Thanks!

修改

好了,我试图以下内容:

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(); 

但升返回0结果假设在decomposedList(1)我找​​星期一。
我创建B1包含周一和周二。

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

推荐答案

使用按位与运算符&安培; 到您想要的标志与实际的标志在数据库相结合,然后检查一个非零的结果。

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);
        }

如果你只是用OR然后组合过滤器值的数组 | 第一:

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);

这篇关于C#EF Linq的按位问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆