与链接/建筑LINQ查询或代替AND [英] Chaining / building LINQ query with an OR instead of AND

查看:111
本文介绍了与链接/建筑LINQ查询或代替AND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑,以更清楚



如果例如我有这种IQueryable的:

 的DateTime theDate =新日期时间(2015,09,30); 

VAR的查询=从组织在组织
,其中org.DisableOrganisation ==假放;&安培;
org.DisableReports ==虚假
选择新的
{
OrganisationId = org.OrganisationId
};

和后来的方法我想的还是添加到其中,如:

  //检查,看看日期是该月的最后一天。 
如果(theDate.AddDays(1).DAY == 1)
{
//下面的语句引入一个与但我想使这是一个OR。
查询= query.Where(X => x.Frequency ==M);
}

这有效地使我查询...

  VAR的查询=从组织
组织的地方org.DisableOrganisation ==假放;&安培;
org.DisableReports ==假放;&安培;
org.Frequency ==M
选择新的
{
OrganisationId = org.OrganisationId
};



但我想让它...

  VAR的查询=从组织
组织在哪里(org.DisableOrganisation ==假放;&安培;
org.DisableReports ==假)||
org.Frequency ==M
选择新的
{
OrganisationId = org.OrganisationId
};



我如何去代替,使这一个OR?



PS 不能使用PredicateBuilder,因为它本质上是具有的EntityFramework的依赖关系(≥6.0.2),我坚持使用LinqKit EF 4.3.1



解决:感谢到D士丹利(说实话,我以前曾使用过这种形式的解决方案,我只是忘了)

 的DateTime theDate =新日期时间(2015,09,30); 
布尔isEndOfMonth = theDate.AddDays(1).DAY == 1;

VAR的查询=从组织在组织
,其中(org.DisableOrganisation ==假放;&安培;
org.DisableReports ==假)||
(isEndOfMonth&安培;&安培;
pr.ProfileType ==L&放大器;&安培;
pr.Frequency ==M)
选择新的
{
OrganisationId = org.OrganisationId
};


解决方案

您无法通过链接做到这一点 - 你将不得不使用某种形式的表达式生成器,或者只是建立在你原来的过滤器:

  VAR天= theDate.AddDays( 1天; 

VAR的查询=从组织在组织
,其中(org.DisableOrganisation ==假放;&安培;
org.DisableReports ==假)||
(==天1和;&放大器;
org.ProfileType ==L,&放大器;&放大器;
org.Frequency ==M)
选择新
{
OrganisationId = org.OrganisationId
};


Edited to be clearer.

If for example I have this IQueryable:

DateTime theDate = new DateTime(2015, 09, 30);

var query = from org in Organisations
            where org.DisableOrganisation == false &&
              org.DisableReports == false
            select new
            {
                OrganisationId = org.OrganisationId
            };

and later in the method I want to add an OR to it, e.g.

// Check to see if the date is the last day of the month.
if (theDate.AddDays(1).Day == 1)
{
    // The following statement introduces an AND but I want to make this an OR.
    query = query.Where(x => x.Frequency == "M");
}

This effectively makes my query...

var query = from org in Organisations
            where org.DisableOrganisation == false &&
              org.DisableReports == false &&
              org.Frequency == "M"
            select new
            {
                OrganisationId = org.OrganisationId
            };

but I want to make it...

var query = from org in Organisations
            where (org.DisableOrganisation == false &&
              org.DisableReports == false) ||
              org.Frequency == "M"
            select new
            {
                OrganisationId = org.OrganisationId
            };

How do I go about making this an OR instead of an AND ?

P.S. Cannot use PredicateBuilder as it is essentially LinqKit which has a dependency of EntityFramework (≥ 6.0.2), I'm stuck using EF 4.3.1

SOLVED: Thanks to D Stanley (To be honest I had used this form of solution before, I simply forgot about it).

DateTime theDate = new DateTime(2015, 09, 30);
bool isEndOfMonth = theDate.AddDays(1).Day == 1;

var query = from org in Organisations
            where (org.DisableOrganisation == false &&
              org.DisableReports == false) ||
              (isEndOfMonth &&
               pr.ProfileType == "L" && 
               pr.Frequency == "M")
            select new
            {
                OrganisationId = org.OrganisationId
            };

解决方案

You can't do it by chaining - you'll have to use some sort of expression builder, or just build that in your original filter:

var day = theDate.AddDays(1).Day;

var query = from org in Organisations
            where (org.DisableOrganisation == false &&
                   org.DisableReports == false) ||
                  (day == 1 &&
                   org.ProfileType == "L" && 
                   org.Frequency == "M")
            select new
            {
                OrganisationId = org.OrganisationId
            };

这篇关于与链接/建筑LINQ查询或代替AND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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