选择对象,并添加到列表中的LINQ C# [英] Select object and add to list linq c#

查看:66
本文介绍了选择对象,并添加到列表中的LINQ C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个简单的Q,但仍然在这一个初学者,不知道如何....
每个工作站可以有若干个发票,所以我下面code将....

存储中的所有工作站,通过每个工作站,结果
获取该工作站的最后一个(最近)的发票,结果
如果发票日期(最近的发票)为< 12月

将它添加到站点列表中...

编辑:
   感谢所有帮助家伙,但我想通过C#这样做,并避免LINQ搜索器你们提到...感谢大家谁回答...

我的新的问题是,我需要进行排序ChosenInvoices列表分为asceding秩序,返回第一个...因为我认为这是在列表中选择任何人:

  VAR allWorkSites =
            (从db.Work_Sites工地
             选择工作场所).Distinct()了ToList()。
    清单<对象> chosenInvoices =新的List<对象>();    的foreach(在allWorksites Work_Site工地)
    {
        发票lastInvoice = worksite.Invoices.LastOrDefault();        如果(lastInvoice!= NULL)
        {
            如果(lastInvoice.Invoice_Date< D​​ateTime.Now.AddMonths(-12))
            {
                chosenInvoices.Add(工作站);
            }
        }
    }


解决方案

 列表<发票和GT; actualInvoices = db.Work_Stations.Distinct()
        。凡(W =方式> w.Invoices.Last()Invoice_Date< D​​ateTime.Now.AddMonths(-12)),选择(W => w.Invoices.Last())。了ToList();

这code将返回最近的发票清单,从每个工作站。

要在asscending顺序列表进行排序您的发票有一个方法的OrderBy(),所以用这种方法顺序,然后占据第一位。

也有名单的排序()方式。

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);
            }
        }
    }

解决方案

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.

这篇关于选择对象,并添加到列表中的LINQ C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆