转换列表LINQ表达式定义列表类(Convert a list linq expression to

2019-10-16 18:23发布

y具有该类

Private Class MyClass
    Public Property propertyOne() as String
    Public Property propertyTwo() as String
    Public Property propertyN() as Integer
End Class

现在,我想从拉姆达或LINQ表达填补MyClass的名单,一些像这样....

    Dim myClassList as new List(Of MyClass)
    myClassList = (From lOtherList1 in MyOtherList1.GetAll()
                   join lOtherList2 in MyOterhList2.GetAll() on lOtherList1.Id Equals lOtherList2.Id
                   Select myClassList.Add(new MyClass With { .propertyOne = lOtherList1.Field1, 
                  .propertyTwo = lOtherList1.Field2,
                  .propertyN = lOtherList2.Field1 })).Tolist()

但我得到这个错误“表达式不会产生一个值”,我该怎么做呢?

Answer 1:

myClassList.Add是在查询错误部分,编辑如下:

Dim myClassList as new List(Of MyClass)
myClassList = (From lOtherList1 in MyOtherList1.GetAll()
               join lOtherList2 in MyOterhList2.GetAll() 
               on lOtherList1.Id Equals lOtherList2.Id
               Select new MyClass With 
               { 
               .propertyOne = lOtherList1.Field1, 
               .propertyTwo = lOtherList1.Field2,
               .propertyN = lOtherList2.Field1 
               })).Tolist()


Answer 2:

你会做到以下几点:

myClassList = (From lOtherList1 in MyOtherList1.GetAll()
               Join lOtherList2 in MyOtherList2.GetAll()
               On lOtherList1.Id Equals lOtherList2.Id
               Select new MyClass With
               {
                   .propertyOne = lOtherList1.Field1,
                   .propertyTwo = lOtherList1.Field2,
                   .propertyN = lOtherList2.Field1
               }).ToList()

你几乎拥有了正确的代码。 你只需要删除调用myClassList.Add()



文章来源: Convert a list linq expression to Defined list Class