开发者

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 JobMeter开发者_StackOverflowID { 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.

Is this possible using 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.


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?)

You can absolutely query an in-memory collection using LINQ, although your questions seems to relate to database.

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.

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. 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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜