使用Linq在“多个选择”中添加条件

时间:2019-03-10 02:15:06

标签: c# .net

我有以下代码

 var reportQuery = new Func<IQueryable<Bid>, IEnumerable<ApprovalRequestDashboardVM>>(bids =>
                            {                                
                                return Bid.GetScopeAndPricingDependencies(bids).SelectMany(bid =>
                                {
                                    var bidWorkflowApprovalValues = bid.ApprovalWorkflows.Values.Where(a => a.StatusHasAlwaysBeenNotEffective == false)
                                                                       .OrderBy(a => a.Name)
                                                                       .ThenBy(a => a.Node.NodeType)
                                                                       .ThenBy(a => a.Node.NodeName);
                                    return bidWorkflowApprovalValues.SelectMany(aw =>
                                    {
                                        return aw.StatusHistory.Where(history => filterApprovalStatus.Count() > 0 ? filterApprovalStatus.Contains(history.StatusID) : true).Select(history =>
                                        {
                                            return new ApprovalRequestDashboardVM()
                                            {
                                                ApprovalID = aw.ApprovalID,
                                                ApprovalName = aw.Name,
                                                BidID = bid.BidID,
                                                BidVersionNumber = bid.BidVersionNumber,
                                                BidStatus = bid.Status,
                                                ApprovalStatus = history.Status,
                                                StatusDate = history.StatusDate,


                                            };
                                        });
                                    });
                                });
                            });

我正在尝试根据特定条件过滤出价

   public bool AllApprovalWorkflowsApproved()
        {
            if (this.ApprovalWorkflows.Values.Where(x => x.Status == ApprovalStatus.Reviewing).Count() > 0)
            {
                return false;
            }
            else
                return true;
        }

因此,如果ApprovalStatus具有Reviweing,那么我不希望出价被处理。

如果我将其添加到查询中

bids = bids.Where(x => x.AllApprovalWorkflowsApproved());确保“审核状态”中仅包含没有工作流程的出价,然后我得到一个错误,即EF无法将我的方法转换为存储表达式。

有人可以告诉我如何过滤SelectMany中的数据

谢谢

1 个答案:

答案 0 :(得分:-1)

class Program
{
    static void Main(string[] args)
    {
        string[] array =
        {
            "king",
            "queen",
            "throne"
        };

        var result = array.SelectMany(element => element.ToCharArray())
            .Where(RemoveLetterTSpec)
            .Where(RemoveLetterESpec);

        foreach (char letter in result)
        {
            Console.WriteLine(letter);
        }

        Console.ReadLine();
    }

    private static bool RemoveLetterESpec(char arg)
    {
        return arg != 'e';
    }

    private static bool RemoveLetterTSpec(char arg)
    {
        return arg != 't';
    }
}

这是一个有关如何对SelectMany进行过滤的简单示例