How to return more than one Database Entity in a LIST?

0

I have two entities, CATEGORY and TRANSACTION , in the database I also have these two tables.

TABELA: CATEGORIA
CHAVE: ID

TABELA: TRANSACAO
CHAVE: CATEGORIA_ID

When done a select by entity performing a join :

var _result = (from a in contenxt.TRANSACAO
                          join b in contenxt.CATEGORiAs on a.CATEGORIA_ID equals b.
                          select new
                          {
                             a.ID,
                             b.TIPO,
                             b.ID
                           });

How do I store this in LIST ?

I would like to do the manipulation of the data and I do not know how to do it, since generally I return a LIST of a specific entity, but I do not know how to do it when they are mixed.     

asked by anonymous 25.01.2015 / 01:17

2 answers

0

I believe your problem is to return this List from a method.

You are doing a query, whose type is anonymous type . You can not create a method that returns a list of an anonymous type.

I suggest you create a class with these 3 information you want to return. Then you can do something like this:

List<CategoriaTransacao> tuaLista = (from a in contenxt.TRANSACAO
                      join b in contenxt.CATEGORiAs on a.CATEGORIA_ID equals b.
                      select new CategoriaTransacao
                      {
                         TransacaoId = a.ID,
                         CategoriaTipo = b.TIPO,
                         CategoriaId = b.ID
                       }).ToList();

In the above example I have a class CategoriaTransacao which has 3 properties, equivalent to what you want to return.

So you can return in a method.

    
25.01.2015 / 15:51
2

In fact, it's a problem. Since I've never used anything so I do not know if it has a better solution than the ones below.

You can create a class to receive this new type created by this query , hence you would return List<Resultado> if this class calls Resultado .

This is because the way you are creating an anonymous type to receive the result, then you do not know what type it is until the execution.

It does not have to be a class itself. You can create a tuples with the composition of this type , it could use something like List<Tuple<tipoID, tipoTIPO, tipoID>> . C # 7 has tuple in the language and its use is usually more interesting.

You can declare this explicitly as a list and access the fields as list elements. It only works if all fields are of the same type. Of course you can use List<object> to accept any type but it loses the type guarantee.

Finally you can return to consider that the return of your function is List<object> , after all the anonymous type created is certainly a object . It loses strong typing but it is a solution. You can still return List<dynamic> but I doubt it is best to return object .

I'll use the example on this page to show what happens to the result and what types we're talking about:

using System.Console;
using System.Linq;
using System.Collections.Generic;

class Customer {
    public int ID { get; set; }
    public string Name { get; set; }
}

class Order {
    public int ID { get; set; }
    public string Product { get; set; }
}

public static class Program {
    public static void Main() {
        // Example customers.
        var customers = new Customer[] {
            new Customer{ID = 5, Name = "Sam"},
            new Customer{ID = 6, Name = "Dave"},
            new Customer{ID = 7, Name = "Julia"},
            new Customer{ID = 8, Name = "Sue"}
        };

        // Example orders.
        var orders = new Order[] {
            new Order{ID = 5, Product = "Book"},
            new Order{ID = 6, Product = "Game"},
            new Order{ID = 7, Product = "Computer"},
            new Order{ID = 8, Product = "Shirt"}
        };

        // Join on the ID properties.
        var query = from c in customers
                join o in orders on c.ID equals o.ID
                select new { c.Name, o.Product };

        WriteLine(query.GetType());

        // Display joined groups.
        foreach (var group in query) {
            WriteLine("{0} bought {1}", group.Name, group.Product);
        }
        var lista = query.ToList();
        WriteLine(lista.GetType());

        //criando explicitamente uma lista de string (neste caso é possível)
        var query2 = from c in customers
                join o in orders on c.ID equals o.ID
                select new List<string> { c.Name, o.Product };

        WriteLine(query2.GetType());


        foreach (var group in query2) {
            WriteLine("{0} bought {1}", group[0], group[1]);
        }
        var lista2 = query2.ToList();
        WriteLine(lista2.GetType());
    }
}

See working on dotNetFiddle .

Example using an explicit class . It conforms to what I said.

I emphasize that there may be a better solution but my lack of experience prevents me from stating. If I remember something I update here.

    
25.01.2015 / 07:06