Using a partial class property inside LINQ statement

后端 未结 6 870
灰色年华
灰色年华 2020-12-05 10:39

I am trying to figure out the best way to do what I thought would be easy. I have a database model called Line that represents a line in an invoice.

It looks roughly

6条回答
  •  情歌与酒
    2020-12-05 11:14

    Try something like this:

    var invoices =
        (from c in _repository.Customers
        where c.Id == id
        from i in c.Invoices
        select new 
        {
            Id = i.Id,
            CustomerName = i.Customer.Name,
            Attention = i.Attention,
            Lines = i.Lines,
            Posted = i.Created,
            Salesman = i.Salesman.Name
        })
        .ToArray()
        .Select (i =>  new InvoiceIndex
        {
            Id = i.Id,
            CustomerName = i.CustomerName,
            Attention = i.Attention,
            Total = i.Lines.Sum(l => l.Total),
            Posted = i.Posted,
            Salesman = i.Salesman
        });
    

    Basically this queries the database for the records you need, brings them into memory and then does the summing once the data is there.

提交回复
热议问题