iT邦幫忙

0

Lambda如何同時使用&&、|| 擷取資料

如題,若以下列範例,我若要擷取出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();
        }
    }
你這樣的邏輯是 x = 1 或 3 且 x > 6
要改用 || x > 6
leo226 iT邦新手 4 級 ‧ 2021-05-23 16:26:33 檢舉
謝謝提點,是我範例寫錯了,修正原文程式,改寫如下,用原本|| 和 && 來測試就可以得到正確的結果了~

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();
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//得正確預期結果
var result = temp.Where(x => (x.a == 1 || x.b == 1) && x.c == "TypeB" && x.d == "StyleB").ToList();
Start:
2
3
6
//以下這個結果,驗證邏輯有點看不懂,為什麼結果裡會有 1, 5的結果出現?
var result = temp.Where(x => x.a == 1 || x.b == 1 && x.c == "TypeB" && x.d == "StyleB").ToList();
Start:
1
2
3
5
6
因為 你沒把 x.a == 1 || x.b == 1 括起來
所以會變成 x.a == 1 或 x.b == 1 且 x.c == TypeB、 x.d == StyleB
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
緯大啊緯大人
iT邦研究生 1 級 ‧ 2021-05-24 00:57:43

結論...
邏輯和數學運算雷同,沒括號和有括號天差地別
符號也有它的優先權

我要發表回答

立即登入回答