2011-04-11 14 views
0

我爲自定義對象創建了一個自定義類。我實例像這樣:將動態生成的對象放置在數組(或集合)中?

Dim class_info as New class_level 

然後我使用LINQ到實體查詢拉一些信息:

Dim get_config = From p in dbContext.Configs _ 
    Where p.Type = "classdate" _ 
    Select p 

然後,我有,我想創建一個實例For..Each循環從這個信息的對象是這樣的:

For Each row In get_config 
     Dim the_row = row 
     class_info.class_level = the_row.Description 
     class_info.dateo = the_row.dateValue 
     class_info.timeo = the_row.timeValue 
     class_info.datec = the_row.dateValue 
     class_info.timec = the_row.timeValue 
     class_info.query = "q" + the_row.Description 
    Next 

現在,我有兩個問題。首先,現在這只是重寫相同的對象屬性。我需要以某種方式動態地命名對象。然後,我需要放置在一個數組這些對象,所以我可以遍歷它們...

回答

3

你真正應該做的是項目的查詢到直接類型:

Dim class_infos = From p in dbContext.Configs _ 
        Where p.Type = "classdate" _ 
        Select New class_level With { 
        .Prop = p.Prop 
        ' other properties here 
        } 

那麼你將結束IEnumerableclass_level,我認爲是你想要的?

你應該考慮命名事情好一點,因爲它很難理解你在這裏工作。

+1

+1使用命名準則。請查看本網站的命名指南:http://msdn.microsoft.com/en-us/library/ms229042.aspx以及其他最佳實踐:http://msdn.microsoft.com/zh-cn/library/h63fsef3 .aspx – 2011-04-11 14:57:34

+0

當我添加多個屬性時,我遇到了intellisense認爲我仍在使用LINQ-to-Entities代碼(p.Prop)的問題,有沒有辦法告訴它我不是? – davemackey 2011-04-11 15:12:52

+1

你的確在使用LINQ-2-SQL。如果你在結果上調用'ToList()','AsEnumerable()'或'ToArray()',LINQ-2-SQL查詢將被評估和執行,你將把LINQ提供者切換到內存提供者。 – 2011-04-11 15:19:31