我認爲設定減法運算符是一個沒有它,它應該在已經完成的方式一個更清潔的方式。我已經給出了方法調用和linq表達方式。
using System;
using System.Collections.Generic;
using System.Linq;
namespace Project
{
public enum Marble { Red, Green, Blue}
public class Bag {
public string Name;
public List<Marble> contents;
}
class Program
{
static void Main()
{
var marbles = new[] { Marble.Red, Marble.Green };
var bags = new[]
{
new Bag {Name = "Foo", contents = new List<Marble> {Marble.Blue}},
new Bag {Name = "Bar", contents = new List<Marble> {Marble.Green, Marble.Red}},
new Bag {Name = "Baz", contents = new List<Marble> {Marble.Red, Marble.Green, Marble.Blue}},
new Bag {Name = "Foo", contents = new List<Marble> {Marble.Blue}},
new Bag {Name = "Bar", contents = new List<Marble> {Marble.Green, Marble.Red}},
new Bag {Name = "Fiz", contents = new List<Marble> {Marble.Red, Marble.Green}},
new Bag {Name = "REDS", contents = new List<Marble> {Marble.Red, Marble.Red}},
new Bag {Name = "Biz", contents = new List<Marble> { Marble.Red } },
new Bag {Name = "Griz", contents = new List<Marble> {Marble.Green, Marble.Green, Marble.Blue}},
new Bag {Name = "Baz", contents = new List<Marble> {Marble.Red, Marble.Green, Marble.Blue}}
};
// the method call version with set substraction operator
var query_v2 = bags.Where(bag => bag.contents.Except(marbles).Count() == 0 &&
marbles.Except(bag.contents).Count() == 0
);
// print out the results
Console.WriteLine("query_v2...");
foreach (var bag in query_v2)
{
Console.WriteLine(bag.Name);
}
Console.WriteLine();
// Follwowing is a LINQ Expression version
var linqversion = from bag in bags
let diff1 = bag.contents.Except(marbles).Count()
let diff2 = marbles.Except(bag.contents).Count()
where diff1 == 0 && diff2 == 0 // perfect match ?
select bag;
Console.WriteLine("Linq expression version output...");
foreach (var bag in linqversion)
{
Console.WriteLine(bag.Name);
}
Console.ReadLine();
}
}
}
我一直在想這個問題。至於爲什麼第二個地方需要,這裏是一個反例。 '新包{名稱=「商務」,內容=新列表 {Marble.Red}}'。據我所知,他正在尋找等值。 –
2010-09-25 02:46:38
@Jeff的確如此。我的解決方案也算不上2個紅軍。 :o – 2010-09-25 03:01:05