這個當前的項目更多地是一個大型項目的概念驗證。我有兩個「表格」(表示如下列表),CatList
和DogList
,它們具有相同的列結構。我希望能夠Concat
或Union
這兩個並使用LINQ
對結果集執行查詢。我被告知通過執行interfaces
我將能夠實現這些結果。LINQ中的Concat/Union表格
這是我到目前爲止有:
static void Main(string[] args)
{
System.Console.Write("Hello World\n");
Dog Dog1 = new Dog { name = "A", age = 1 };
Dog Dog2 = new Dog { name = "B", age = 2 };
Cat Cat1 = new Cat { name = "C", age = 3 };
Cat Cat2 = new Cat { name = "D", age = 4 };
List<Dog> DogList = new List<Dog>();
List<Cat> CatList = new List<Cat>();
DogList.Add(Dog1);
DogList.Add(Dog2);
CatList.Add(Cat1);
CatList.Add(Cat2);
var result = DogList
.Concat(CatList);
}
}
public interface iAnimal
{
string name { get; set; }
int age { get; set; }
}
public class Dog :iAnimal
{
public string name { get; set; }
public int age { get; set; }
}
public class Cat:iAnimal
{
public string name { get; set; }
public int age { get; set; }
}
我可以訪問任何的任何使用類似System.Console.Write(Dog1.name);
我創建的對象的數據成員的,所以我覺得我實現的接口是正確的。爲了有效地Concat /聯合我的列表,需要在LINQ語句中更改哪些內容。
我對C#
和麪向對象的編程一般都比較陌生,所以請迎合您的答案以適應我的知識水平。
感謝
編輯 我很抱歉,我忘了包括錯誤消息我在當前的代碼獲得。
Error 1 'System.Collections.Generic.List<InterfaceTest.Dog>' does not contain a definition for 'Concat' and the best extension method overload 'System.Linq.ParallelEnumerable.Concat<TSource>(System.Linq.ParallelQuery<TSource>, System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments
Error 2 Instance argument: cannot convert from 'System.Collections.Generic.List<InterfaceTest.Dog>' to 'System.Linq.ParallelQuery<InterfaceTest.Cat>'
對不起,忘記了包含錯誤。問題是固定的。 – Jeff