0

我想获得一组债券及其相关的到期时间,其中的到期时间.matamount > 200000。

编辑:(我只希望每个债券的到期集合包括到期时间> 200000)

这是一个 program.cs,它具有类 defs 和一个为查询填充测试数据的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqToObjects
{
class Program
{
    static void Main(string[] args)
    {
        Program.QueryCollection();
        Console.ReadLine();
    }
    public static void QueryCollection()
    {
        List<Bond> bonds = Program.BuildCollections();
        //how do I get a list of Bonds that have a maturity.MatAmount > 200,000?
    }
    public static List<Bond> BuildCollections()
    {
        List<Bond> bonds = new List<Bond>();
        Bond bond;

        for (int i = 1; i <= 10; i++)
        {
            bond = new Bond() {ID = i, Title = "Bond Title " + i.ToString() };
            for (int j = 1; j <= 10; j++)
            { 
                bond.Maturities.Add(new Maturity(){ID = j, BondID = i, MatDate = DateTime.Today.AddDays(j), MatAmount = 152000 * j});
            }

            bonds.Add(bond);
        }
        return bonds;
    }
}
public class Bond
{
    public int ID { get; set; }
    public string Title { get; set; }
    public List<Maturity>  Maturities { get; set; }
    public Bond()
    {
        Maturities = new List<Maturity>();
    }
}
public class Maturity
{
    public int ID { get; set; }
    public int BondID { get; set; }
    public DateTime MatDate { get; set; }
    public int MatAmount { get; set; }
}

}
4

2 回答 2

1

这个怎么样?

IEnumerable<Bond> bigBonds = bonds.Where(b => b.Maturities.Any(m => m.MatAmount > 200000));
于 2012-09-26T23:35:27.833 回答
0

我不确定您在寻找什么,但要获得您的债券,只需执行以下操作:

var filteredBonds = 
        (
            from bond in bonds
            join maturity in maturities on bond.ID equals maturity.BondID
            where maturity.MatAmount > 200000
            select new { bond.ID, bond.Title, maturity.MatAmount, maturity.MatDate }
        ).ToList();

ToArray() 是可选的,如果您愿意,可以将内容保留在表格中。我不确定你想对你的结果做什么。

您可能还想在数据结构中解耦债券和到期日。我不认为你真的需要一个成熟度列表,因为你将 BondID 存储为成熟度对象的成员。这似乎有点多余,但老实说,我不使用 Linq,所以也许这就是要走的路。你的来电。

于 2012-09-26T23:29:51.783 回答