Linq两个左外连接错误非静态方法需要一个目标

时间:2015-03-05 11:28:45

标签: c# linq linq-to-entities linqpad

我无法执行此错误"非静态方法需要目标。"我不知道为什么我会遇到这个错误。 下面的查询中有四个表,其中一个连接和两个左外连接。最后一个左边的连接可能包含一个匹配的空行

var model = from p in Uow.Instance.RepoOf<RoleMenuMetrix>().GetAll()
                    from n in Uow.Instance.RepoOf<NavigationMenu>().GetAll().Where(q => q.Id == p.MenuId)
                    from m in
                        Uow.Instance.RepoOf<NavigationButton>()
                            .GetAll()
                            .Where(q => q.NavigationMenuId == n.Id)
                            .DefaultIfEmpty()
                    from o in // when I comment this from it works fine, but i donot get the values from this table.
                         Uow.Instance.RepoOf<RoleButtonMatrix>().GetAll().Where(q => q.ButtonId == m.Id).DefaultIfEmpty()
                    where p.RoleId == Guid.Parse("96246E99-6BF2-4A3D-8D2C-263DDEF2F97B")
                          && n.IsActive && n.ApplicationName == "MEM"
                    select new
                    {
                        p.MenuId,
                        p.RoleId,
                        n.Name,
                        n.ParentId,
                        ButtonName = m != null ? m.ButtonName : String.Empty,
                        ButtonId = m != null ? m.Id : 0,
                         ischecked = o.RoleId == "96246E99-6BF2-4A3D-8D2C-263DDEF2F97B" ? "true" : "false"//// when I comment this  it works fine, but i do not get the values from this table
                    };

错误屏幕拍摄它是从模型变量生成的 enter image description here

下面是所需的预期输出

enter image description here

1 个答案:

答案 0 :(得分:0)

表达式,重新格式化以使事情更清晰:

Uow.Instance.RepoOf<RoleButtonMatrix>()
            .GetAll()
            .Where(q => q.ButtonId == m.Id)
            .DefaultIfEmpty()

用于填充o。但是1.它看起来像是返回一个对象,而不是一个集合; 2.传递给DefaultIfEmpty的集合为空时为空。

由于需要集合的from子句,您会收到错误消息。我希望您可以放弃DefaultIfEmpty运算符,当没有任何符合您标准的内容时,让LINQ使用空集合。