2012-05-26 28 views
1

y具有該類轉換列表LINQ表達式定義列表類

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() 

但我得到這個錯誤「表達式不會產生一個值」,我如何做到這一點?

回答

0

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() 
0

你會做到以下幾點:

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()