How convert result of linq query to List(Of T) - vb.net

I'm using EntityFrameWork 5 in VB MV4 project.
I have a database built from EntityFramework diagram (model firt as opposed to code first)
I have a ViewModel X, containing a List(ofT) T being one on my Entity
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.
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
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.
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

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)

Related

Error when creating a List object in VB.Net

I am new to .Net framework and I am getting an error message
Value of type 'List(Of AdminSetEmployeeParams)' cannot be converted to
'AdminSetEmployeeParams'"
Dim SetNewEmployee As New List(Of AdminSetEmployeeParams)
SetNewEmployee.Add(New AdminSetEmployeeParams With {
.departmentId = ddlDept.SelectedValue,
.familyName = txtLastOrSurname.Text,
.firstName = txtFirstOrGivenName.Text,
.secondName = txtSecondName.Text,
.contactPhone = txtPhone.Text,
.user = ""})
SetNewEmployee = EmployeeAPIService.AdminSetEmployee(SetNewEmployee).Result
How would I resolve this error?
Update:
System.Threading.Tasks
Public Class Task(Of TResult)
Public ReadOnly Property Result As TResult
It's probably this, but it's hard to be sure:
Dim newEmployee As New AdminSetEmployeeParams
newEmployee = New AdminSetEmployeeParams With {
.departmentId = ddlDept.SelectedValue,
.familyName = txtLastOrSurname.Text,
.firstName = txtFirstOrGivenName.Text,
.secondName = txtSecondName.Text,
.contactPhone = txtPhone.Text,
.user = ""})
Dim newEmployeeResult As List(Of AdminSetEmployeeParams) = EmployeeAPIService.AdminSetEmployee(newEmployee).Result
I'm not sure why your API returns a list of employees in its result (I'm assuming it does because you don't say you have an error message complaining about the assignment of the result to a list(of...) ) but the error as given would reasonably only occur if the API call demanded a single new employee and you handed it a list of new employees
If it doesn't work out, try this last line
Dim newEmployeeResult As AdminSetEmployeeParams = EmployeeAPIService.AdminSetEmployee(newEmployee).Result
And if that doesn't work out, edit your question to give more info on what kind of arguments AdminSetEmployee takes and what kind of object .Result is

MVC pass two objects to view

I am just struggling with passing two objects to my view and then show data out of it. On purpose to send two diffrent objects to a view i maked separated class for it which would contain my two class object to be pased as follows:
Public Class CustomModelProjetsTransports
Public Projects As IEnumerable(Of woitgroup_transport.tbProjekt)
Public Transports As IEnumerable(Of woitgroup_transport.tbTransport)
End Class
Now in my controlled i am pasing by it to a view (i checked and the data is there:
Function Index() As ActionResult
If Not IsNothing(Session("LogedUserId")) Then
Dim userId As Integer = Session("LogedUserId")
Dim projectsAndTransportsLists As New CustomModelProjetsTransports
Dim ProjectsPerUser As New List(Of tbProjekt)
ProjectsPerUser = db.Database.SqlQuery(Of tbProjekt)("SELECT * FROM [mydb].[dbo].[tbProjekt] where Id IN (SELECT DISTINCT ProjectId FROM [mydb].[dbo].[tbUserProject] WHERE UserId = " & Session("LogedUserId") & ")").ToList
Dim transportsPerUser As New List(Of tbTransport)
transportsPerUser = db.Database.SqlQuery(Of tbTransport)("SELECT * FROM [mydb].[dbo].[tbTransport] where ProjectId IN (SELECT DISTINCT ProjectId FROM [mydb].[dbo].[tbUserProject] WHERE UserId = " & Session("LogedUserId") & ")").ToList
projectsAndTransportsLists.Projects = ProjectsPerUser
projectsAndTransportsLists.Transports = transportsPerUser
Return View(projectsAndTransportsLists)
Else
Return RedirectToAction("Index", "Login")
End If
End Function
problem is here within View:
this is on top:
#ModelType IEnumerable(Of woitgroup_transport.CustomModelProjetsTransports)
<tbody>
#For Each item In Model(1).Transports.ToList
Dim currentItem = item
#<tr>
...
when i am executing my application its filling out correctly here:
Return View(projectsAndTransportsLists)
but then i am reciving an error when view wants to be shown as follows:
The model item passed into the dictionary is of type 'woitgroup_transport.CustomModelProjetsTransports', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[woitgroup_transport.CustomModelProjetsTransports]'.
Additional question:
Kepping on mind i have Projects list within my view how to correctly fill in dropdown (the best using bootstrap styles). I am trying like that but still something wrong...
#Html.DropDownListFor(Function(m) m.Projects.., DirectCast(Model.Projects, SelectList), New With { _
.class = "form-control" _
})
its also saying that Function(m) m.Projects.Name
that Name is not exist - i see only generic list on intelisense like Function(m) m.Projects.ToList ...
The error message is quite clear about the problem. You are passing a CustomModelProjetsTransports to the view. But the view expects a IEnumerable(Of CustomModelProjetsTransports). Remove the IEnumerable and you should be fine.

WCF EF return as list

Hi I got the error when return EF as the list. Here are my codes.
WCF
Public Function GetMerchantList() As List(Of Merchant) Implements IMerchant.GetMerchantList
Dim ws As New aMerchantService.MerchantServiceClient
Dim General As New General
Dim kWSUrl As String = ""
Dim endpointAddress = ws.Endpoint.Address
Dim newEndpointAddress As New EndpointAddressBuilder(endpointAddress)
kWSUrl = General.ConvertWsURL("App")
newEndpointAddress.Uri = New Uri(kWSUrl & "MerchantService.svc")
ws = New aMerchantService.MerchantServiceClient("BasicHttpBinding_IMerchantService", newEndpointAddress.ToEndpointAddress())
Dim Data = ws.GetMerchantList()
Return Data
End Function
Merchant Class
Public Function GetMerchantList() As List(Of Merchant)
Dim Db As New TTMSEntities
Dim Data = (From p In Db.TT_MERCHANT Join r In Db.TT_BRANCH_SETTING On _
p.MERCHANT_BRANCH_INTERNAL_NUM Equals r.INTERNAL_NUM _
Select New Merchant With {.MerchantID = p.MERCHANT_ID,
.MerchantName = p.DESCRIPTION,
.BranchID = r.INTERNAL_NUM,
.BranchName = r.BRANCH_DESC})
If Data IsNot Nothing Then
Return Data.ToList
Else
Return Nothing
End If
End Function
The error is Error Value of type '1-dimensional array of
TTMS.App.WebSites.Data.Merchant' cannot be converted to
'System.Collections.Generic.List(Of TTMS.Web.WebSites.WCF.Merchant)'.
Please help. Thanks
It looks like you're using a service reference. By default, WCF will serialize generic lists as arrays. To override this behavior, when you go to add the service reference, click on the Advanced button at the bottom left corner. This will bring up the Service Reference Settings. Select System.Generics.List for the collection type (the default is System.Array):

LINQ to XML - select to a strongly typed object (VB)

This is not a big issue but it's bugging me. I know how to select into a strongly typed collection (List(Of T), but I can't find a tidy way of doing this for an object that is not a List.
This works:
Dim x = From a In response...<artist> _
Select New MBArtistInfo With {.MBID = a.#id, .Name = a.<name>.Value, .Gender = a.<gender>.Value}
Return x(0)
but it is annoying to have to do that.
I have seen a C# solution elsewhere along the lines of:
var x = from a In response...<artist>
select new MBArtistInfo
{
MBID = etc
but I can't convert this to VB.
Has anyone done this?
Just skip class name:
Dim x = From a In response...<artist> _
Select New With { .MBID = a.#id, .Name = a.<name>.Value, .Gender = a.<gender>.Value }
But it will give you IEnumerable(Of T), where T is an anonymous type. You should not return objects like that from your methods:
For example, an anonymous type cannot be used to define a method
signature, to declare another variable or field, or in any type
declaration. As a result, anonymous types are not appropriate when you
have to share information across methods.
Edit
To get only one element from collection use First/FirstOrDefault methods:
Dim x = (From a In response...<artist> _
Select New With { .MBID = a.#id, .Name = a.<name>.Value }).FirstOrDefault();

Only parameterless constructors and initializers are supported in LINQ to Entities

Help me understand why this isn't working:
Dim i = (From f In EfUtil.Db.EMAILADDRESSHISTORY _
Where f.EMAILADDRESS.CUSTOMERCONTACTPERSON.CUSTOMERguid = New Guid(Request.QueryString("customer")) _
Select New With {.guid = f.ACTIONguid, .name = f.ACTIONname}).ToList
I'm getting the following error
Only parameterless constructors and initializers are supported in LINQ
to Entities.
Altough I'm finding a lot of examples where this construction works. What am I missing?
The examples you are finding most likely are LINQ to Objects examples and not LINQ to Entities.
You can work around this by declaring the GUID beforehand:
Dim customerGuid as Guid = New Guid(Request.QueryString("customer"))
Dim i = (From f In EfUtil.Db.EMAILADDRESSHISTORY _
Where f.EMAILADDRESS.CUSTOMERCONTACTPERSON.CUSTOMERguid = customerGuid _
Select New With {.guid = f.ACTIONguid, .name = f.ACTIONname}).ToList
To be clear: The problem here is not your Select. The problem is the New Guid(...) in the Where condition.