Службы RIA: включить с некоторыми (многие) не работает - PullRequest
2 голосов
/ 20 января 2012

У меня есть два типа сущностей: Document (имеет Customer) и Customer (имеет коллекцию Documents)

Мой запрос заключается в получении документов для клиента на основе либоимя или номер клиента.

Запрос выглядит следующим образом:

public IQueryable<Document> GetCustomerDocuments(DateTime startDate, DateTime endDate, string filterText)
{
    return this.ObjectContext.Customers
              .Where(c => c.CustomerName.Contains(filterText) || c.CustomerNumber.Contains(filterText))
              .SelectMany(c => c.Documents)
              .Where(d => d.Date >= startDate && d.Date <= endDate);
}

Когда запрос вернется, я хочу, чтобы он включал ОБА Document и Customer сущностей ....

Я перепробовал все, что мог, включая Include("Documents.Customer"), Include("Customer") и т. Д.

У меня определенно установлен IncludeAttribute в метаданных.

Мысли?Это вообще возможно?

Спасибо!

Ответы [ 2 ]

4 голосов
/ 20 января 2012

Вместо использования проекции и SelectMany я написал запрос LINQ, используя соединение:

        var v = from cust in (from c in this.ObjectContext.Customers
                where  (c.CustomerName.Contains(filterText) || c.CustomerNumber.Contains(filterText))  select c)
                join doc in this.ObjectContext.Documents on cust.CustomerNumber equals doc.CustomerNumber
                where doc.Date >= startDate && doc.Date <= endDate
                select doc;
        return ((ObjectQuery<Document>)v).Include("Customer").AsQueryable<Document>();

Это решает проблему!

0 голосов
/ 20 января 2012

.Include работает от ObjectQuery, и его эффект будет отменен при добавлении любой пользовательской проекции.Вы можете попробовать следующие варианты:

Перепишите запрос в терминах Document:

return this.ObjectContext.Documents.Include("Customers")
          .Where(d => d.Customers.Any(c => 
                                      c.CustomerName.Contains(filterText) 
                                   || c.CustomerNumber.Contains(filterText))
          .Where(d => d.Date >= startDate && d.Date <= endDate);

Это может работать, а может и не работать, а может и не генерировать приличный sql;для тестирования.

Другая возможность - определить объект DTO

class CustomerDocument
{
   public Customer {get;set;}
   public Document {get;set;}
}

Тогда ваш запрос станет:

return from c in this.ObjectContext.Customers
       from d in c.Documents
       where (c.CustomerName.Contains(filterText) 
          || c.CustomerNumber.Contains(filterText))
           && d.Date >= startDate && d.Date <= endDate
       select new CustomerDocument {Customer = c, Document = d};
...