【问题标题】:How to ignore particular condition in LinQ如何忽略 LinQ 中的特定条件
【发布时间】:2017-05-12 10:03:08
【问题描述】:

我有一个如下的 linq:

var finalist = (from b in GeneralAction.EM().Vwmembersummarydetails
                where selectedPersonId.Contains(b.Id)
                select new GeneralCommonFunctions.MemberDetailSumary
                {
                  Index = 0,
                  ProjectText = string.Join(", ", (from d in GeneralAction.EM().Member_has_properties
                                                   join e in GeneralAction.EM().Properties on d.Property_id equals e.Id
                                                   where d.IsDeleted == 0 && d.Member_id == b.Id && e.ProjectType_id == Convert.ToInt32(projectTypeId)
                                                   select e.Project.Description).Distinct().ToArray()),
                  PropertyText = string.Join(", ", (from f in GeneralAction.EM().Member_has_properties
                                                    join g in GeneralAction.EM().Properties on f.Property_id equals g.Id
                                                    where f.IsDeleted == 0 && f.Member_id == b.Id && g.ProjectType_id == Convert.ToInt32(projectTypeId)
                                                    select g.LotNum).ToArray()),
                  PurchasePrice = GeneralCommonFunctions.GetTotalPurchasePriceByProjectType(b.Id, projectTypeId),
                  Name = b.Name,
                  Email = b.Email,
                  }).AsQueryable();

我想问一下,如何忽略这两个条件:

e.ProjectType_id == Convert.ToInt32(projectTypeId)

g.ProjectType_id == Convert.ToInt32(projectTypeId)

当 projectTypeId 为 null 或 0 时。

【问题讨论】:

标签: conditional-statements


【解决方案1】:

要忽略条件,只需使用逻辑或:

(projectTypeId == null || Convert.ToInt32(projectTypeId) == 0 || e.ProjectType_id == Convert.ToInt32(projectTypeId))

要调用不同的函数,请使用三级运算符。请注意,这可能不适用于 Linq to SQL -

(projectTypeId == null ? GeneralCommonFunctions.GetTotalPurchasePrice(b.Id) : GeneralCommonFunctions.GetTotalPurchasePriceByProjectType(b.Id, projectTypeId))

【讨论】:

    【解决方案2】:

    我是投反对票的人。 SO的原因是寻找真正难以找到的解决方案。但是好像你在发帖之前都没有尝试过。

    用你自己的话解决办法——

    e.ProjectType_id == Convert.ToInt32(projectTypeId)

    g.ProjectType_id == Convert.ToInt32(projectTypeId)

    当 projectTypeId 为 null 或 0 时。

    所以,把那个英文句子转换成代码。

    && e.ProjectType_id == Convert.ToInt32(projectTypeId)
    

    会变成-

    && ((e.ProjectType_id != null && e.ProjectType_id != "0") ? e.ProjectType_id == Convert.ToInt32(projectTypeId) : true)
    

    然后您可以像@netmage 提到的那样对其进行更多优化-

    (projectTypeId == null || Convert.ToInt32(projectTypeId) == 0 || e.ProjectType_id == Convert.ToInt32(projectTypeId))
    

    同样,

    projectTypeId 为 null 时如何调用 GeneralCommonFunctions.GetTotalPurchasePrice(),projectTypeId 不为 null 时如何调用 GeneralCommonFunctions.GetTotalPurchasePriceByProjectType()。

    类似地变成-

    PurchasePrice = projectTypeId = null ? GeneralCommonFunctions.GetTotalPurchasePrice(...) : GeneralCommonFunctions.GetTotalPurchasePriceByProjectType(b.Id, projectTypeId),
    

    【讨论】:

    • 我发现我的问题实际上是重复的。无论如何感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多