Linq到对象C#
我想获得到期日大于200000的债券集合及其相关到期日集合 编辑:(我只希望每个债券的到期日集合包括>200000的到期日) 下面是一个program.cs,它具有类defs和一个用于填充查询测试数据的方法Linq到对象C#,c#,linq-to-objects,C#,Linq To Objects,我想获得到期日大于200000的债券集合及其相关到期日集合 编辑:(我只希望每个债券的到期日集合包括>200000的到期日) 下面是一个program.cs,它具有类defs和一个用于填充查询测试数据的方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinqToObjects { class Program { static void M
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间LinqToObjects
{
班级计划
{
静态void Main(字符串[]参数)
{
Program.QueryCollection();
Console.ReadLine();
}
公共静态void QueryCollection()
{
List bonds=Program.BuildCollections();
//如何获取到期日为.MatAmount>200000的债券列表?
}
公共静态列表BuildCollections()
{
列表债券=新列表();
债券;
对于(int i=1;i我不确定你在寻找什么,但要获得你的债券,只需做如下操作:
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()是可选的,如果您愿意,可以将内容保留在表格中。我不确定您想对结果做什么。
您可能还希望在数据结构中分离债券和到期日。我认为您不需要有到期日列表,因为您将债券ID存储为到期对象的一个成员。这似乎有点多余,但老实说,我不使用Linq,所以可能这就是方法。您的电话。
这个怎么样
IEnumerable<Bond> bigBonds = bonds.Where(b => b.Maturities.Any(m => m.MatAmount > 200000));
IEnumerable bigBonds=债券,其中(b=>b.到期日.Any(m=>m.MatAmount>200000));
你有问题吗?有些东西不起作用了吗?哇,这个解决方案让mines大吃一惊。这绝对是最简单的方法。我不确定性能是否更好,但这很容易理解和使用(如果你不清楚=>查找lambda函数)。@BenjaminDangerJohnson很高兴你喜欢它:-)LINQ有时会失控,但我认为它在这种情况下工作得很好。Matthew,感谢您提供了这一行简洁的代码。我想知道您是否可以修改代码,只返回mats>200k的债券,但到期日集合应该只包含那些>200k的到期日。我正在筛选这些到期日,因此不包括这些到期日“所有到期日”。如果您想这样做,我建议您创建一个委托方法而不是lambda函数。请尝试以下操作:`public void SomeMethod(){IEnumerable bigBonds=bonds.Where(FilterFunc);}private bool FilterFunc(Bond b){bool result=false;List filteredMaturities=b.durities.Where(m=>.MatAmount>200000.ToList();if(filteredMaturities.Count>0){b.durities=filteredMaturities;result=true;}返回结果;}`请注意,上面的代码将改变您原来的绑定对象。您可能希望在调用该列表之前克隆该列表。解决方法是在绑定上删除“Where”,并使用函数以原始对象为基础创建新的绑定对象,然后执行“Where”"关于原始债券的到期日。感谢本杰明,我喜欢马特的简短。你能看看我对马特的后续问题吗?我一直在考虑从债券中提取集合。如果你这样做,你只会得到超过适当价值的债券和到期日。但请注意,它们不会在债券/到期类别中S格式,它们将被存储为DATAROW(如果您想考虑切换到数据库,这将是有用的)。