简体   繁体   English

具有嵌套集合的自定义对象

[英]Custom object with nested collection

I've created an object that contains another collection in one of it properties. 我创建了一个在其中一个属性中包含另一个集合的对象。

This is the main object: 这是主要对象:

public class MeterPrevReadInfo
{
    public int JobMeterID { get; set; }
    public string PreviousJobReference { get; set; }
    public FuelType MeterFuelType { get; set; }
    public List<MeterPrevReadRegInfo> Regs { get; set; }
    public DateTime DateMeterRead { get; set; }
}

This is the child object: 这是子对象:

public class MeterPrevReadRegInfo
{
    public string RegisterID { get; set; }
    public string MeterRead { get; set; }
}

I need to bind this object to a repeater control, I would like to show the DateMeterRead property and all the MeterRead properties in the repeater. 我需要将此对象绑定到转发器控件,我想显示转发器中的DateMeterRead属性和所有MeterRead属性。

Is this possible using Linq? 使用Linq可以吗? I could easily do it using a t-sql query from the database, but I just figured it should be possible to do this in memory without the overhead of another trip to the database. 我可以使用数据库中的t-sql查询轻松地做到这一点,但我只是想应该可以在内存中做到这一点,而又不必再花钱去访问数据库了。

Don't get confused - LINQ isn't a data access layer or ORM (perhaps you're thinking of LINQ-to-SQL, or LINQ-to-Entities?) 请勿混淆-LINQ不是数据访问层或ORM(也许您正在考虑LINQ-to-SQL或LINQ-to-Entities?)

You can absolutely query an in-memory collection using LINQ, although your questions seems to relate to database. 尽管您的问题似乎与数据库有关,但是您可以使用LINQ绝对查询内存中的集合。

I could easily do it using a t-sql query from the database, but I just figured it should be possible to do this in memory without the overhead of another trip to the database. 我可以使用数据库中的t-sql查询轻松地做到这一点,但我只是想应该可以在内存中做到这一点,而又不必再花钱去访问数据库了。

You can retrieve all this data from the database in one query & then construct objects. 您可以通过一次查询从数据库中检索所有这些数据,然后构造对象。 You can do this with a stored procedure, LINQ-to-SQL, Entity Framework, or other tools. 您可以使用存储过程,LINQ-to-SQL,实体框架或其他工具来执行此操作。 You should choose the best tool for your requirements. 您应该根据自己的需求选择最佳工具。 I expect this is a very small part of the requirement, so take a step back, choose the best tool, and make this work using that tool. 我希望这只是需求的一小部分,因此请退后一步,选择最佳工具,然后使用该工具进行工作。

sure this is possible. 确保这是可能的。 It looks like you want something like this: 看起来您想要这样的东西:

 List<MeterPrevReadInfo> list = ...;

 var result = from item in list
              from info in item.Regs
              select new {item.DateMeterRead, info.MeterRead};

This query defines a list of anonymous objects with the two properties you want. 此查询定义具有两个所需属性的匿名对象的列表。

You can access an anonymous object representing the model you want by using the Linq Select extension method as follows: 您可以使用Linq Select扩展方法来访问表示所需模型的匿名对象,如下所示:

var readInfo = new MeterPrevReadInfo();
readInfo.Regs.Select(x => new {
                                        x.RegisterID, 
                                        x.MeterRead, 
                                        readInfo.DateMeterRead
                                       });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM