1

probably a simple Q but still a beginner at this and not sure on how to.... Each WorkStation can have a number of Invoices So my below code will....

store all the workStations, go Through each workStation,
get the last (most recent) invoice for that workStation,
If the invoices date (for the most recent Invoice) is < 12 Months

Add it to the list of sites...

EDIT: Thanks for all the help guys but I am trying to do it through c# and avoid the LINQ searche you guys have mentioned...thanks for everyone who replied...

My new problem being i need to sort the ChosenInvoices list into asceding order and return the first...as i think it is selecting anyone in the list:

 var allWorkSites =
            (from worksites in db.Work_Sites
             select worksites).Distinct().ToList();
    List<Object> chosenInvoices = new List<Object>();

    foreach (Work_Site worksite in allWorksites)
    {
        Invoice lastInvoice = worksite.Invoices.LastOrDefault();

        if (lastInvoice != null)
        {
            if (lastInvoice.Invoice_Date < DateTime.Now.AddMonths(-12))
            {
                chosenInvoices.Add(workstation);
            }
        }
    }
17
  • add what to the list of sites? the invoice object or the workstation object? Commented Jan 24, 2013 at 13:28
  • what is list of sites? There nothing about it in your example Commented Jan 24, 2013 at 13:28
  • @ryadavilli add the invoice object thanks Commented Jan 24, 2013 at 13:30
  • @Ph0en1x sorry about that meant to be workstation Commented Jan 24, 2013 at 13:31
  • There is something off about your class/list design, you want go through all workstations and then get last invoice for each and then add an invoice to your list of workstations ? You need better naming of your classes, or need to relook at your logic. Commented Jan 24, 2013 at 13:32

5 Answers 5

4
List<invoice> actualInvoices = db.Work_Stations.Distinct()
        .Where(w => w.Invoices.Last().Invoice_Date < DateTime.Now.AddMonths(-12)).Select(w => w.Invoices.Last()).ToList();

This code will return you the list of most recent invoices from each workstation.

To sort your invoice in asscending order list have a method OrderBy(), so order it with this method and then take the first one.

Also list have Sort() method.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think you can be absolutely sure that Last() will return the element with the most recent date.
Think it's hard to say for sure because we know just a little about it. I still do not 100% sure that get the question right.
2
allWorkStations
    .Where(w => w.Invoices.Last().Invoice_Date < DateTime.Now.AddMonths(-12))
    .Select(w => list.add(w));

Or better yet:

List<Work_Station> list = db.Work_Stations
    .Distinct()
    .Where(w => w.Invoices.Last().Invoice_Date < DateTime.Now.AddMonths(-12))
    .ToList();

Comments

1
var allWorkStations =
            (from workstation in db.Work_Stations
             where workstation.Invoices.Last().Invoice_Date < DateTime.Now.AddMonths(-12)
             select workstation).Distinct();

Comments

1

The following code will create list of workstations which had invoice in last year

var checkDate = DateTime.Now.AddMonths(-12);

var resultList = db.Work_Stations
  .Distinct()
  .Select(ws => new {Ws = ws, Li = ws.Invoices.OrderBy(i => i.Invoice_Date).LastOrDefault()})
  .Where(item => item.Li != null && Li.Invoice_Date < checkDate)
  .Select(item => item.Ws)
  .ToList();

Comments

1

This will work even if the invoices are not in date order:

invoiceLst.AddRange(allWorkStations
    .Where(w => w.Invoices.Max(i => i.Invoice_Date) < DateTime.Now.AddMonths(-12)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.