如何使用linq从类中的类列表中获取值

时间:2013-03-19 02:24:20

标签: c# linq

我想访问类内部列表中的类成员值。我的课程看起来像这样。 SureViewEvents类包含传入警报的父级详细信息,SureViewEventDetails是警报的详细值。我想使用LINQ循环遍历类SureViewEvents并循环遍历详细信息行(如果有的话)。

我该怎么做呢?

public class SureViewEvents
{
    public string EventId { get; set; }
    public string GroupId { get; set; }
    public string DateTimeCreated { get; set; }
    public string GroupTitle { get; set; }
    public string EventTitle { get; set; }
    public string SubscriberId { get; set; }
    public bool Closed { get; set; }

    public List<SureViewEventDetails> Details { get; set; }
}

//EventID|EventRecordID|CreatedDate|EventRecordTypeID|Details|Input1|Input2|EventCode|SubscriberID|EventTitle|SerialNo
public class SureViewEventDetails
{
    public string EventId { get; set; }
    public string EventRecordID { get; set; }
    public string CreateDate { get; set; }
    public string EventRecordTypeID { get; set; }
    public string Details { get; set; }
    public string Area { get; set; }
    public string Zone { get; set; }
    public string EventCode { get; set; }
    public string SubscriberID { get; set; }
    public string EventTitle { get; set; }
    public int SerialNo { get; set; }
    public bool Handled { get; set; }
}

我可以使用以下方法检索父级别值,但我不确定如何访问使用此结构填充的详细信息。任何建议表示赞赏!

var activeEvents = (from sve in m_sureViewEvents 
                    select sve).ToList();

lock (m_driverLock)
{
    foreach (var activeEvent in activeEvents)
    {
        if (activeEvent.Closed == false)
        {
            m_fepConnector.HandleAlarms();
            DownloadZipArchive(activeEvent.EventId);
            CloseSureViewEvent(activeEvent.EventId);
        }
    }
}

2 个答案:

答案 0 :(得分:5)

您可以使用SelectMany选择所有详细信息:

foreach (var details in activeEvents.SelectMany(e => e.Details))
{
    // ... Stuff
}

这将获得所有SureViewEventDetails中的所有activeEvents

答案 1 :(得分:1)

在每个activeEvent的循环中,您可以简单地遍历细节:

foreach (var activeEvent in activeEvents)
{
    foreach (var eventDetail in activeEvent.Details)
    {
        // do something with the detail
    }
}