给出以下枚举
[Flags]
public enum Trans{
Purchase =1,
Sale=2,
All = Purchase | Sale
};
以下课程
public class Person
{
public string Name;
public Trans TransType;
}
以下数据结构
var people = new List<Person>();
people.Add(new Person {Name="James", TransType = Trans.Purchase});
people.Add(new Person {Name="Nancy", TransType = Trans.Sale});
people.Add(new Person {Name="Heather", TransType = Trans.All});
people.Add(new Person {Name="Duane", TransType = Trans.All});
我正在尝试查询符合特定条件的人的列表。
让所有人参加Trans.Purchase(James,Heather&amp; Duane)
var query = from p in people where ( (p.TransType & Trans.Purchase) == Trans.Purchase) select p;
让Trans.Sale(Nancy,Heather&amp; Duane)的所有人
var query = from p in people where ( (p.TransType & Trans.Sale) == Trans.Sale) select p;
我需要在LINQ查询的where子句中指定什么来返回 Trans.Purchase,Trans.Sale 或两者(即Trans.All)的所有人)? (詹姆斯。南希,希瑟和杜安)
修改 一些上下文 - TransType存储在数据库中,我试图编写一个用户指定TransType的屏幕,一个SQL查询按上面的指定提取数据
答案 0 :(得分:10)
您可以使用此查询:
var query = from p in people
where (p.TransType.HasFlag(Trans.Purchase) || p.TransType.HasFlag(Trans.Sale))
select p;
查询也可以这样写:
var query = from p in people
where ((p.TransType & Trans.All) > 0)
select p;