How to return multiple classes as IQueryable <T>

Using Linq, I would like to return an object containing the customers and invoices that they have.

I understand that one method from a method returns:

public IQueryable<customers> GetCustomers()
{
    return from c in customers
           select c;
}

But it’s hard for me to understand a few objects:

public IQueryable<???> GetCustomersWithInvoices()
{
    return from c in customers
           from inv in c.invoices
           select new {c, ci}  // or I may specify columns, but rather not.
}

I have a feeling that I am approaching this wrong. The goal is to call these objects from the controller and pass them to the view, either direct or using the formViewModel class.

+3
source share
3 answers

. , . , , ( , ), .

, , .

+1

, "" . , .

, , .

- scottgu . :

, CLR , . , , / LINQ.

.

+1

, , .

public IQueryable<T> GetCustomersWithInvoices(T exampleObject)
{
return from c in customers
from inv in c.invoices
select new {c, ci} // or I may specify columns, but rather not.
}

var exampleObject = new {
Customer c = new Customer(),
Invoice i = new Invoice()
};

var returnedObjectOfAnonymousType = GetCustomersWithInvoices(exampleObject);

That way you can use type inference to return your method to an anonymous type. You should use this ugly transfer method in an example object to make it work. I really do not recommend you to do this, but I believe that this is the only way to do this.

0
source

Source: https://habr.com/ru/post/1733466/


All Articles