繁体   English   中英

使用LINQ,如何从List中找到具有给定属性值的对象?

[英]Using LINQ, how do I find an object with a given property value from a List?

我有一个名为Questions的课程。 这个Questions有属性QuestionIDQuestionAnswer 在foreach中迭代这个List of Question ,我必须找到.QuestionID = 12 如果我找到.QuestionID = 12那么我必须立即为.QuestionAnswer = "SomeText" .QuestionID = 14 .QuestionAnswer = "SomeText"赋值。

我不想再次在.QuestionId = 12' to find内再次迭代以再次.QuestionId = 12' to find .QuestionID = 14`。

有什么方法可以直接使用LINQ?来使用.QuestionID = 14

例如:

For Each mQuestion As Question In _mQuestions
    If mQuestion.QuestionId = 12 Then
         'Find mQuestion.QuestionID= 14 and insert Somtext to 
          'mQuestion.QuestionAnswer="SomeText"
    End IF
Next

我想你正在寻找这样的东西。 如果我有片刻,我会将其翻译成VB,但我认为你可以关注。

if (_mQuestions.Any(q => q.QuestionID == 12)) 
{
   Question question14 = _mQuestions.FirstOrDefault(q => q.QuestionID == 14);
   if (question14 != null)
       question14.QuestionAnswer = "Some Text";
}

不幸的是,您的数据结构( List )要求您在找到Question-14 Question-12再次搜索以查找Question-14 如果您的Question列表按ID排序,则可以进行一些改进,但一般情况下,只能通过了解元素属性的值来直接访问ListArray的元素。

适用于您的问题的数据结构是Dictionary因为它允许通过某些值索引对象,以及有效直接访问这些对象而无需遍历整个集合。

您可以通过调用ToDictionary()扩展方法将您的列表转换为使用Linq的字典:

IDictionary<Question> questions = _mQuestions.ToDictionary(q => q.id);

它使用Question对象的ID作为键,将对象作为值。 然后在您的代码中,您可以执行以下操作:

if (questions.ContainsKey(12) && questions.ContainsKey(14))
{
    questions[14].QuestionAnswer = "Some Text";
}

请注意, ContainsKey和索引器(operator [])都在恒定时间内执行。

请注意,我对它的看法比其他样本更加冗长,但我设法做的只有LINQ。 (注意下面的C风格评论!)

Private Shared Sub Main(args As String())
    Dim questions As List(Of Question) = GetQuestions()
    Dim question As Question = ( _
        Where q.ID = 14 AndAlso _
              questions.Exists(Function(p) p.ID = 12)).FirstOrDefault()
    If question IsNot Nothing Then
        question.Answer = "Some Text"
    End If
End Sub



 // Build the collection of questions! We keep this simple.
Private Shared Function GetQuestions() As List(Of Question)
    Dim questions As New List(Of Question)()
    questions.Add(New Question(12, "foo"))
    questions.Add(New Question(14, "bar"))
    Return questions
End Function

// You've already got this class. This one is just my version.
Public Class Question
    Public Sub New(id As Integer, answer As String)
        Me.ID = id
        Me.Answer = answer
    End Sub
    Public Property ID() As Integer
        Get
            Return m_ID
        End Get
        Set
            m_ID = Value
        End Set
    End Property
    Private m_ID As Integer
    Public Property Answer() As String
        Get
            Return m_Answer
        End Get
        Set
            m_Answer = Value
        End Set
    End Property
    Private m_Answer As String
End Class

使用LINQ:

var listOfQ = new List<Question>();

// populate the list of Question somehow...

var q14 = listOfQ.FirstOrDefault(q => q.QuestionID == 14);
if (listOfQ.Any(q => q.QuestionID == 12) && q14 != null)
{
    q14.QuestionAnswer = "SomeText";
}

暂无
暂无

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

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