0

我有两个有关系的表,我想用两个条件从它查询,如下所示:

var data = from d in db.table1
           where !d.isdeleted & d.table2.Where(dm => !dm.isdeleted)
           select d;

但我有这个错误:

运算符“&”不能应用于“bool”和“System.Collections.Generic.IEnumerable”类型的操作数

谢谢

4

3 回答 3

1

你应该做两个改变

1. 更改&&&(这不是必需的,但在您希望在所有情况下都执行第二个操作数之前是一个很好的做法)

2. 更改IEnumerable<T> (or IQueryable<T>)bool。而不是Where,使用Any

var data = from d in db.table1
       where !d.isdeleted && d.table2.Any(dm => !dm.isdeleted) 
       select d;
于 2013-01-10T02:50:21.937 回答
1

也许这就是你所追求的?

var data = from d in db.table1
           where !d.isdeleted && d.table2.All(dm => !dm.isdeleted)
           select d;

这将从table1whereisdeleted为 false 的行中选择行,并且所有相应的行都table2等于isdeletedfalse。

于 2013-01-10T00:50:16.263 回答
1

如果它们是相关的,你可以加入表格......

public class Program
{
    private static void Main(string[] args)
    {
        List<ItemA> itemATable = new List<ItemA>(new[]
            {
                new ItemA {Id = "A1", IsDeleted = true},
                new ItemA {Id = "A2", IsDeleted = false},
            });

        List<ItemB> itemBTable = new List<ItemB>(new[]
            {
                new ItemB {Id = "B1", ItemAId = "A1", IsDeleted = true},
                new ItemB {Id = "B2", ItemAId = "A1", IsDeleted = false},
                new ItemB {Id = "B3", ItemAId = "A2", IsDeleted = true},
                new ItemB {Id = "B4", ItemAId = "A2", IsDeleted = false},
            });

        var test = from a in itemATable
                   join b in itemBTable on a.Id equals b.ItemAId
                   where !a.IsDeleted && !b.IsDeleted
                   select new { a, b };
    }
}

public class ItemA
{
    public string Id { get; set; }
    public bool IsDeleted { get; set; }
}

public class ItemB
{
    public string Id { get; set; }
    public string ItemAId { get; set; }
    public bool IsDeleted { get; set; }
}
于 2013-01-10T02:35:10.197 回答