Skip to main content
deleted 131 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Entity Framework quiries optimizationinvoice queries

I have these queries written in entityframeworkEntity Framework where I try to get:

  • NameofName of the customer
  • Most Recent Invoice Ref
  • Most Recent Invoice Amount (£)
  • Number of outstanding invoices (#)
  • Total of all outstanding invoices (£)

I WOULD BE SO THANKFUL IF YOU HELP ME TO MAKE THEM MORE EFFICIENT. I WOULD APPRECIATE ANY SUGGESTION.

I use these two tables:

And the queries aII make are:

ALso hereHere is the implementation of AllINcluding:

public virtual IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = _entitiesContext.Set<T>();

        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }
        return query;
    }

    

Entity Framework quiries optimization

I have these queries written in entityframework where I try to get

  • Nameof the customer
  • Most Recent Invoice Ref
  • Most Recent Invoice Amount (£)
  • Number of outstanding invoices (#)
  • Total of all outstanding invoices (£)

I WOULD BE SO THANKFUL IF YOU HELP ME TO MAKE THEM MORE EFFICIENT. I WOULD APPRECIATE ANY SUGGESTION.

I use these two tables

And the queries aI make are:

ALso here is the implementation of AllINcluding

public virtual IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = _entitiesContext.Set<T>();

        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }
        return query;
    }

    

Entity Framework invoice queries

I have these queries written in Entity Framework where I try to get:

  • Name of the customer
  • Most Recent Invoice Ref
  • Most Recent Invoice Amount (£)
  • Number of outstanding invoices (#)
  • Total of all outstanding invoices (£)

I use these two tables:

And the queries I make are:

Here is the implementation of AllINcluding:

public virtual IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = _entitiesContext.Set<T>();

        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }
        return query;
    }
Source Link

Entity Framework quiries optimization

I have these queries written in entityframework where I try to get

  • Nameof the customer
  • Most Recent Invoice Ref
  • Most Recent Invoice Amount (£)
  • Number of outstanding invoices (#)
  • Total of all outstanding invoices (£)

I WOULD BE SO THANKFUL IF YOU HELP ME TO MAKE THEM MORE EFFICIENT. I WOULD APPRECIATE ANY SUGGESTION.

I use these two tables

CREATE TABLE Customers
(
    CustomerId INT,
    Name NVARCHAR(100),
    Address1 NVARCHAR(100),
    Address2 NVARCHAR(100),
    Postcode NVARCHAR(100),
    Telephone NVARCHAR(15),
    CONSTRAINT Customers_PK PRIMARY KEY (CustomerId)
)

CREATE TABLE Invoices
(
    InvoiceId INT,
    CustomerId INT, -- FK
    Ref NVARCHAR(10),
    InvoiceDate DATETIME,
    IsPaid BIT,

    Value DECIMAL,
    CONSTRAINT Invoices_PK PRIMARY KEY (InvoiceId)
)

And the queries aI make are:

public PaginatedList<CustomerListEntity> GetCustomers(int pageIndex, int pageSize, out int totalRecords)
        {
            var customers = _customerRepository.AllIncluding(x => x.Invoices).OrderBy(x => x.CustomerId).Select(x => new
              CustomerListEntity {
                Name = x.Name,
                CustomerID=x.CustomerId,
                RecentInvoiceRef = x.Invoices.OrderByDescending(t => t.InvoiceDate).FirstOrDefault() != null ? x.Invoices.OrderByDescending(t => t.InvoiceDate).FirstOrDefault().Ref : string.Empty,
                RecentInvoiceAmount = x.Invoices.OrderByDescending(t=>t.InvoiceDate).FirstOrDefault()!=null? x.Invoices.OrderByDescending(t => t.InvoiceDate).FirstOrDefault().Value:null,
                UnpaidInvoicesNumber=x.Invoices.Where(t=>t.IsPaid==false).Count(),
                UnpaidInvoicesTotalAmount = x.Invoices.Where(t => t.IsPaid == false).Sum(k=>k.Value)
            });
            totalRecords = customers.Count();
            return customers.ToPaginatedList(pageIndex, pageSize);
        }

ALso here is the implementation of AllINcluding

public virtual IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = _entitiesContext.Set<T>();

        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }
        return query;
    }