开发者

How to use Linq to select information from a collection into another concrete class I made?

开发者 https://www.devze.com 2023-03-25 04:31 出处:网络
I have the following class: public class EntityJESummary { public int JEGroupingId { get; set; } public int PartnershipId { get; set; }

I have the following class:

public class EntityJESummary
{
    public int JEGroupingId { get; set; }
    public int PartnershipId { get; set; }
    public int JEId { get; set; }
    public DateTime BookingDate { get; set; }
    public DateTime EffectiveDate { get; set; }
    public bool Allocated { get; set; }
    public int JEEstate { get; set; }
    public float Debit { get; set; }
    public float Credit { g开发者_运维百科et; set; }
    public string JEComments { get; set; }

    public EntityJESummary()
    {

    }
}

And here I'm using Linq to filter out DataRows from a source. I'm trying to fit information from this datasource into this new holder type class.

Any suggestions?

_dttMasterViewTransaction = dtsTransaction.Tables["tblTransaction"];

var datos = _dttMasterViewTransaction.AsEnumerable()
         .Where(r => r["JEID"] == FundsID)
         .Select(new EntityJESummary ???

Notice where I'm using r["foo"], I'm fetching data from each DataRow. I need to get specific rows and fit them into specific properties of my holder class.

Also, in the data table, there might be many rows for a single JEId, so I'd like to grab each Debit from each datarow and Sum it into the float Debit property.

Any suggestions would be very much appreciated. :)


Untested but try something similar to what you did with the Where clause:

var datos = _dttMasterViewTransaction.AsEnumerable()
.Where(r => r["JEID"] == FundsID)
.Select(r => new EntityJESummary { 
      JEGroupingId = r["JEGroupingId"],
      PartnershipId = r["PartnershipId"],
      .....
    } );


You can make use of Object Initalizers.

_dttMasterViewTransaction = dtsTransaction.Tables["tblTransaction"];

var datos = _dttMasterViewTransaction.AsEnumerable()
    .Where(r => r["JEID"] == FundsID).Select(r =>
        new EntityJESummary() {
            JEGroupingId = r["JEID"],
            PartnershipId = r["PartnershipId"]
        };
0

精彩评论

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