編輯: 由於relatedCodes
是int[]
類型(我使用Code
類型的數組)的溶液看起來略有不同,但不太多的:
var relatedCodes = new int[2] { 2, 4 };
var joinedCodes = from ac in AllCodes
join rc in relatedCodes on ac.Id equals rc
select ac;
ORIGINAL答案
一種可能性是使用連接:
void Main()
{
var AllCodes = new List<Code>()
{
new Code() {Id = 1, Description="Foo1"},
new Code() {Id = 2, Description="Bar2"},
new Code() {Id = 3, Description="Foo3"},
new Code() {Id = 4, Description="Bar4"}
};
var relatedCodes = new Code[2]
{
new Code() {Id = 2, Description="Bar2"},
new Code() {Id = 4, Description="Bar4"}
};
var joinedCodes = from ac in AllCodes
join rc in relatedCodes on ac.Id equals rc.Id
select ac;
joinedCodes.Dump();
}
// Define other methods and classes here
public class Code{
public int Id { get; set; }
public string Description { get; set; }
}
輸出繼電器:
東西哪裏,並可包含。你有什麼嘗試? – CodeCaster 2014-09-30 06:47:14
這不應該太難,請您提供迄今爲止嘗試過的代碼示例嗎? – Maritim 2014-09-30 06:47:49