vb.net 如何将 linq 查询的结果转换为 List(Of T)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18855184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How convert result of linq query to List(Of T)
提问by Benoit Letourneau
I'm using EntityFrameWork 5 in VB MV4 project.
我在 VB MV4 项目中使用 EntityFrameWork 5。
I have a database built from EntityFramework diagram (model firt as opposed to code first)
我有一个从 EntityFramework 图构建的数据库(模型优先而不是代码优先)
I have a ViewModel X, containing a List(ofT) T being one on my Entity
我有一个 ViewModel X,其中包含一个 List(ofT) T,它是我的实体中的一个
When I open my web application (on the browser) I ask a controller to give me the ViewModel X as a Json object that I use to populate a MVVC (knockout model) using the Knockout JS Mapping pluggin.
当我打开我的 Web 应用程序(在浏览器上)时,我要求控制器将 ViewModel X 作为 Json 对象提供给我,我用它来使用 Knockout JS 映射插件填充 MVVC(敲除模型)。
When I ask for the model, I populate it using code similar to what is shown below
当我要求模型时,我使用类似于下面显示的代码填充它
Public Class DataServiceController
Inherits System.Web.Mvc.Controller
<Authorize()> _
Public Function RetrieveData() As JsonResult
Dim model As ViewModelX
model = New ViewModelX
'=====================================
' Resources and Tools
'=====================================
Dim fetchedResourceAndToolsQuery = From a In db.ResourceAndTools
Where a.ProfileId = profile.ProfileId Select a
For Each eachRes In fetchedResourceAndToolsQuery
Dim res As ResourceAndTools = New ResourceAndTools
res.Name = Trim(eachRes.Name)
res.URL = Trim(eachRes.URL)
res.Target = eachRes.Target
res.ResourceId = eachRes.ResourceId
model.ResourceAndTools.Add(res)
Next
Return Json(model, JsonRequestBehavior.AllowGet)
Everthing works great! Except... And here's the question
一切都很好!除了......这是问题
As mentionned above, ViewModelX contains a List(of T) T being ResourceAndTools
如上所述,ViewModelX 包含一个 List(of T) T 是ResourceAndTools
Is there a was to copy (clone, load, not sure of the term) the content of fetchedResourceAndToolsQuery (Result of the Linq Query) to model.ResourceAndTools (List(of T)) without instantiating a new object and copying the properties like I'm doing.
是否有必要将 fetchedResourceAndToolsQuery(Linq 查询的结果)的内容复制(克隆、加载、不确定术语)到 model.ResourceAndTools(List(of T)),而无需实例化新对象并复制像我这样的属性正在做。
I've seached (Cloning, Deep Copying, Shallow Copying, etc.) The closest I came to a solution was some explanation on how to deep copy an object but it relied on serialisation, it did not work because not all properties of a Entity Framework object are serialisable.
我搜索过(克隆、深复制、浅复制等)我最接近的解决方案是关于如何深复制对象的一些解释,但它依赖于序列化,它不起作用,因为并非实体的所有属性框架对象是可序列化的。
Any help is appreciated Thank you
任何帮助表示赞赏谢谢
采纳答案by Jodrell
Like this?
像这样?
model.ResourceAndTools = (
From a In db.ResourceAndTools
Where a.ProfileId = profile.ProfileId
Select New ResourceAndTools() With { _
.Name = Trim(a.Name), _
.URL = Trim(a.URL), _
.Target = a.Target, _
.ResourceId = a.ResourceId}).ToList()
Following your comment, perhaps you could do,
根据您的评论,也许您可以这样做,
Dim dataList = (
From a In db.ResourceAndTools
Where a.ProfileId = profile.ProfileId
Select New With
{
.Name = Trim(a.Name),
.URL = Trim(a.URL),
.Target = a.Target,
.ResourceId = a.ResourceId
}).ToList()
model.ResourceAndTools = dataList.ConvertAll(Function(data)
Return New ResourceAndTools() With
{
.Name = data.Name,
.Url = data.Url,
.Target = data.Target,
.ResourceId = data.ResourceId
}
End Function)

