LINQ中的Concat/Union表 [英] Concat/Union Tables in LINQ

查看:449
本文介绍了LINQ中的Concat/Union表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该当前项目更多地是大型项目的概念证明.我有两个表"(如下表所示),CatListDogList,它们具有相同的列结构.我希望能够同时使用ConcatUnion并使用LINQ对结果集执行查询.有人告诉我,通过实施interfaces我将能够实现这些结果.

这是我到目前为止所拥有的:

        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);之类的东西来访问任何创建的对象的任何数据成员,因此我认为接口的实现是正确的.为了有效地合并/合并我的列表,需要在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>'

解决方案

问题是编译器无法弄清楚通过将List<Dog>List<Cat>连接起来,它应该产生iAnimal s的集合.默认情况下,它会看到一个List<Dog>,并且希望以下集合也是Dog的集合.

您可以通过向Concat提供通用参数(即

)来明确告知将所有内容都视为iAnimal.

var result = DogList.Concat<iAnimal>(CatList);

然后您将得到类型为IEnumerable<iAnimal>result.

如果您停留在.NET 3.5上,则需要类似

var result = DogList.Cast<IAnimal>().Concat(CatList.Cast<IAnimal>());

3.5无法自动将collection of something that inherits iAnimal转换为collection of iAnimal.

This current project is more of a proof of concept for a larger project. I have two "tables" (represented as lists below), CatList and DogList, that have identical column structure. I wish to be able to Concat or Union the two and perform a query on the resulting set using LINQ. I was told that by implementing interfaces I would be able to achieve these results.

Here is what I have so far:

        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; }
}

I can access any of the data members of any of my created objects using something like System.Console.Write(Dog1.name);, so I THINK my implementation of interfaces is correct. What needs to be changed in my LINQ statement in order to effectively Concat/Union my lists.

I am rather new to C# and object oriented programming in general, so please cater your answers to suit my knowledge level.

Thanks

EDIT I apologize, as I have forgotten to include the error message I get in my current code.

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>'

解决方案

The issue is that the compiler can't figure out that by concatenating a List<Dog> and a List<Cat> it should produce a collection of iAnimals. By default it sees a List<Dog> and expects the following collection to be a collection of Dog too.

You can explicitly tell to treat everything as an iAnimal this by providing the generic parameter to Concat, i.e.

var result = DogList.Concat<iAnimal>(CatList);

You then get a result of type IEnumerable<iAnimal>.

Edit: If you're stuck on .NET 3.5, you'll need something like

var result = DogList.Cast<IAnimal>().Concat(CatList.Cast<IAnimal>());

as 3.5 can't automatically convert collection of something that inherits iAnimal to collection of iAnimal.

这篇关于LINQ中的Concat/Union表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆