Click here to Skip to main content
16,004,974 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: HashSet(Of T) - how can I get elements by position number ? Pin
Sascha Lefèvre15-Apr-15 14:20
professionalSascha Lefèvre15-Apr-15 14:20 
GeneralRe: HashSet(Of T) - how can I get elements by position number ? Pin
dilkonika15-Apr-15 15:55
dilkonika15-Apr-15 15:55 
GeneralRe: HashSet(Of T) - how can I get elements by position number ? Pin
Sascha Lefèvre16-Apr-15 7:24
professionalSascha Lefèvre16-Apr-15 7:24 
GeneralRe: HashSet(Of T) - how can I get elements by position number ? Pin
dilkonika16-Apr-15 7:32
dilkonika16-Apr-15 7:32 
GeneralRe: HashSet(Of T) - how can I get elements by position number ? Pin
Sascha Lefèvre16-Apr-15 8:16
professionalSascha Lefèvre16-Apr-15 8:16 
GeneralRe: HashSet(Of T) - how can I get elements by position number ? Pin
dilkonika16-Apr-15 8:26
dilkonika16-Apr-15 8:26 
GeneralRe: HashSet(Of T) - how can I get elements by position number ? Pin
Sascha Lefèvre16-Apr-15 8:36
professionalSascha Lefèvre16-Apr-15 8:36 
Questionwhy this code doesn't work ? Pin
dilkonika13-Apr-15 6:20
dilkonika13-Apr-15 6:20 
Hello !
This is a code that is supposed to do a copy of an entity framework object with the childs collection that the user want.
Imports System.Data.Objects
Imports System.Data.Objects.DataClasses
Imports System.Runtime.CompilerServices

Public Module Entities
<Extension()>
Public Function CloneEntity(Of T As Class)(entity As T, context As ObjectContext, Optional include As List(Of IncludeEntity) = Nothing, Optional copyKeys As Boolean = False) As T
    Return CloneEntityHelper(entity, context, include, copyKeys)
End Function

Private Function CloneEntityHelper(Of T As Class)(entity As T, context As ObjectContext, Optional include As List(Of IncludeEntity) = Nothing, Optional copyKeys As Boolean = False) As T
If include Is Nothing Then include = New List(Of IncludeEntity)()
Dim myType = entity.GetType()
Dim methodInfo = context.GetType().GetMethod("CreateObject").MakeGenericMethod(myType)
Dim result = methodInfo.Invoke(context, Nothing)
Dim propertyInfo = entity.GetType().GetProperties()
For Each info In propertyInfo
    Dim attributes = info.GetCustomAttributes(GetType(EdmScalarPropertyAttribute), False).ToList()

    For Each attr As EdmScalarPropertyAttribute In attributes 
        If (Not copyKeys) AndAlso attr.EntityKeyProperty
            Continue For
        End If

        info.SetValue(result, info.GetValue(entity, Nothing), Nothing)
    Next
    If info.PropertyType.Name.Equals("EntityCollection`1", StringComparison.OrdinalIgnoreCase) Then
        Dim shouldInclude = include.SingleOrDefault(Function(i) i.Name.Equals(info.Name, StringComparison.OrdinalIgnoreCase))
        If shouldInclude Is Nothing Then Continue For
        Dim relatedChildren = info.GetValue(entity, Nothing)
        Dim propertyType As Type = relatedChildren.GetType().GetGenericArguments().First()
        Dim genericType As Type = GetType(EntityCollection(Of ))
        Dim boundType = genericType.MakeGenericType(propertyType)
        Dim children = Activator.CreateInstance(boundType)
        For Each child In relatedChildren
            Dim cloneChild = CloneEntityHelper(child, context, shouldInclude.Children, shouldInclude.CopyKeys)
            children.Add(cloneChild)
        Next
        info.SetValue(result, children, Nothing)
    End If
    Next
    Return result
    End Function
VB
Public Class IncludeEntity
        Public Property Name As String
        Public Property Children As New List(Of IncludeEntity)
        Public Property CopyKeys As Boolean
    Public Sub New(propertyName As String, ParamArray childNodes() As String)
       Name = propertyName
       Children = childNodes.Select(Function(n) new IncludeEntity(n)).ToList()
End Sub
End Class
End Module

Now , I use the above code like that :
VB
Dim litm, newitm As New MyObject
    Dim inc = New List(Of IncludeEntity)()
    inc.Add(New IncludeEntity("Child_list"))
    litm=context.MyObjects.FirstOrDefault
    newitm = litm.CloneEntity(CType(context, Entity.Infrastructure.IObjectContextAdapter).ObjectContext,include:=inc)

The problem is that nothing is copied.
What's wrong with my code ?
Thank you !
AnswerRe: why this code doesn't work ? Pin
Sascha Lefèvre13-Apr-15 13:56
professionalSascha Lefèvre13-Apr-15 13:56 
GeneralRe: why this code doesn't work ? Pin
dilkonika13-Apr-15 14:11
dilkonika13-Apr-15 14:11 
GeneralRe: why this code doesn't work ? Pin
Sascha Lefèvre13-Apr-15 15:37
professionalSascha Lefèvre13-Apr-15 15:37 
GeneralRe: why this code doesn't work ? Pin
Sascha Lefèvre14-Apr-15 12:56
professionalSascha Lefèvre14-Apr-15 12:56 
QuestionEntity Framework : Clone an object and only its childs Pin
dilkonika11-Apr-15 17:47
dilkonika11-Apr-15 17:47 
AnswerRe: Entity Framework : Clone an object and only its childs Pin
Sascha Lefèvre11-Apr-15 21:02
professionalSascha Lefèvre11-Apr-15 21:02 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
dilkonika12-Apr-15 11:32
dilkonika12-Apr-15 11:32 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
Sascha Lefèvre12-Apr-15 11:40
professionalSascha Lefèvre12-Apr-15 11:40 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
dilkonika12-Apr-15 11:38
dilkonika12-Apr-15 11:38 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
Sascha Lefèvre12-Apr-15 12:09
professionalSascha Lefèvre12-Apr-15 12:09 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
dilkonika12-Apr-15 13:26
dilkonika12-Apr-15 13:26 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
Sascha Lefèvre12-Apr-15 14:39
professionalSascha Lefèvre12-Apr-15 14:39 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
dilkonika12-Apr-15 15:05
dilkonika12-Apr-15 15:05 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
Sascha Lefèvre12-Apr-15 23:18
professionalSascha Lefèvre12-Apr-15 23:18 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
dilkonika13-Apr-15 5:04
dilkonika13-Apr-15 5:04 
GeneralRe: Entity Framework : Clone an object and only its childs Pin
Sascha Lefèvre13-Apr-15 5:30
professionalSascha Lefèvre13-Apr-15 5:30 
QuestionEntity Framework : Why this extension is not usable ? Pin
dilkonika10-Apr-15 18:25
dilkonika10-Apr-15 18:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.