如題,若以下列範例,我若要擷取出1,3,7,8,9的資料,如何使用AND + OR來擷取資料呢?
Console.WriteLine("Start:");
var temp = new int[]{ 1,2,3,4,5,6,7,8,9};
var result = temp.Where(x => (x == 1 || x == 3) && x > 6).ToList();
result.ForEach(x =>
{
Console.WriteLine(x);
});
Console.ReadLine();
public class myModel
{
public int id { get; set; }
public int a { get; set; }
public int b { get; set; }
public string c { get; set; }
public string d { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Start:");
List<myModel> temp = new List<myModel>();
temp.Add(new myModel() { id = 1, a = 1, b = 0, c = "TypeA", d = "StyleA" });
temp.Add(new myModel() { id = 2, a = 1, b = 0, c = "TypeB", d = "StyleB" });
temp.Add(new myModel() { id = 3, a = 0, b = 1, c = "TypeB", d = "StyleB" });
temp.Add(new myModel() { id = 4, a = 0, b = 1, c = "TypeA", d = "StyleA" });
temp.Add(new myModel() { id = 5, a = 1, b = 1, c = "TypeB", d = "StyleA" });
temp.Add(new myModel() { id = 6, a = 1, b = 1, c = "TypeB", d = "StyleB" });
temp.Add(new myModel() { id = 7, a = 0, b = 0, c = "TypeB", d = "StyleB" });
var result = temp.Where(x => (x.a == 1 || x.b == 1) && x.c == "TypeB" && x.d == "StyleB").ToList();
result.ForEach(x =>
{
Console.WriteLine(x.id);
});
Console.ReadLine();
}
}