Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat/Union Tables in LINQ

Tags:

c#

linq

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>'
like image 614
Jeff Avatar asked Jan 08 '13 16:01

Jeff


1 Answers

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.

like image 155
Rawling Avatar answered Sep 24 '22 23:09

Rawling