如何使用Linq从集合中选择信息到我制作的另一个具体类?

时间:2011-08-03 14:51:03

标签: c# linq class sum datarow

我有以下课程:

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 { get; set; }
    public string JEComments { get; set; }

    public EntityJESummary()
    {

    }
}

在这里,我使用Linq从源过滤掉DataRows。我正在尝试将来自此数据源的信息放入这个新的持有者类型类中。

有什么建议吗?

_dttMasterViewTransaction = dtsTransaction.Tables["tblTransaction"];

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

注意我正在使用r [“foo”],我从每个DataRow获取数据。我需要获取特定的行并使它们适合我的持有者类的特定属性。

另外,在数据表中,单个JEId可能有很多行,所以我想从每个数据行中获取每个Debit并将其加到浮点Debit属性中。

非常感谢任何建议。 :)

2 个答案:

答案 0 :(得分:2)

未经测试但尝试类似于您对Where子句所做的操作:

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

答案 1 :(得分:0)

您可以使用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"]
        };