Solved

Entity Framework 4; One Function Calls Another

Posted on 2011-09-20
24
321 Views
Last Modified: 2012-05-12
I am working on a solution looping through a table which is a result set with data from multiple systems. I need to update some columns in the table with additional information. I am using Entity Framework 4 and need to know how to correctly have one function call another.
 
The first function gets all employeeids from the table.
 For each EmployeeID, another function must be called that Adds the FunctionalManager Name to the Table.
 
How do I write this correctly?
 Public Function GetEmployeeID() As IList(Of Table1)
    Dim dataContext = New MyEntities()
    Dim EmployeeIdList = (From n In dataContext.Table1 Select n.EmployeeID).ToList
    For Each n In EmployeeIdList  GetManName(EmployeeID)-Error Here
    Next
    Return EmployeeIdList
End Function

Public Function GetManName(id)
    Dim dataContext = New MyEntities()
    Dim ManName = From n In dataContext.Table1
                Where n.EmployeeID = id
                Select n.FunctionalMgrName.FirstOrDefault

    Return ManName

End Function
0
Comment
Question by:Annette Wilson, MSIS
  • 12
  • 12
24 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36569777
It sounds like you need this:

For Each n In EmployeeIdList
    GetManName(n.EmployeeID)
Next n

Open in new window

0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 36569788
Also, you should be able to use the ForEach operator with the EmployeeIdList, instead of a for...each loop.
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36570729
TheLearnedOne,
This works beautifully looping through the list and allowing me to process one employeeID at a time.

Thank you very much.

Now the most important piece is that as I process each ID, I have to save the Employee Manager Name to the table.  


I created with your help the following two methods in a repository class.
Public Function GetEmployeeID() As System.Linq.IQueryable(Of Table1)
        Dim dataContext = New MyEntities()
        Dim EmpManName As String = " "
        Dim EmployeeIdList = (From n In dataContext.Table1 Select n.EmployeeID).ToList
        For Each n In EmployeeIdList
            GetManID(n.ToString, EmpManName)
        Next n
        Return EmployeeIdList

    End Function

    Public Function GetManID(ByVal id, ByVal EmpManName)
        Dim dataContext = New MyEntities()
        Dim ManName = From n In dataContext.Table1
                    Where n.EmployeeID = id
                    Select n.FunctionalMgrName.FirstOrDefault

        Return ManName

    End Function


My program has API methods that retrieve the the manager name from another system as I pass in the employeeID.

How do I save the string to Table1 

Here's the end of the API method that I am working on:

 Return FuncMgrName = MyAPI.APIStrValue(hQry, 0)

        Dim repository As New CasualDataRepository
        Dim SavMan = repository.GetManID(EmployeeID, FuncMgrName)
        repository.SaveChanges()

Open in new window

0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 36572835
Are you looking for something like this?

        EmployeeIdList.ForEach(Function(x) x.ManagerName = GetManagerID(x.EmployeeID))

Open in new window

0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36573164
Thank you for responding.

The ManagerName value is coming from a call in code from another system.  The get EmployeeID Function goes through the list of employeeID and provides a parameter for the call to the other system.  The call to the other system only allows me to return one value. In this case FuncMgrName.

I'm trying to save the FuncMgrName that comes from the other system into the table on that employeeID's row.  When the value FuncMgrName is saved, I retrieve the next EmployeeID and save that EmployeeID's Manager name.  The code in bold is the value that I am saving to the table.  I just don't know how to correctly save FuncMgrName


Return FuncMgrName = myAPI.StrValue(hQry, 0)

        Dim repository As New CasualDataRepository
        Dim SavMan = repository.GetManID(EmployeeID, FuncMgrName)
        repository.SaveChanges()
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 36573221
This line returns a boolean value (is that what you intended):

Return FuncMgrName = myAPI.StrValue(hQry, 0)

Is dataContext.Table1 the Employee list?  Are you trying to look up the manager ID for the Employee record stored in the employee list?
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36573340
According to the team, the line will return the FuncMgrName... If not I'll make sure it is fixed to return the FuncMgrName. For now, I'll write the code to save that value to Table1

I Can retrieve the EmployeeID and save the necessary values for that row.  I'm iterating through the employeeList and saving the values.  

This function is allowing me to get the correct EmployeeID.  For each EmployeeID, I save the correct value.
Public Function GetEmployeeID() As System.Linq.IQueryable(Of Table1)
        Dim dataContext = New MyEntities()
        Dim EmpManName As String = " "
        Dim EmployeeIdList = (From n In dataContext.Table1 Select n.EmployeeID).ToList
        For Each n In EmployeeIdList
            GetManID(n.ToString, EmpManName) Trying to save the EmployeeManagerValue
        Next n
        Return EmployeeIdList

    End Function



Here's the structure of Table1
id                               AutoGenerated
 EmployeeID                Already Populated from another system
 EmployeeName           Already Populated from another system
 InternalContract         Already Populated from another system
 FuncMgrID                 Already Populated from another system
 FuncMgrName            Need to populate
 FuncMgrEmail            Need to populate
 RecruitMgrName        Need to populate
 RecruitMgrEmail         Need to populate
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36573500
To return a value, you would need something like this:

Return myAPI.StrValue(hQry, 0)

The other form is a boolean expression that evaluates if FuncMgrName is equal to that value.

Return FuncMgrName = myAPI.StrValue(hQry, 0)

What are you getting back from the function?  Where do you need to assign the value?  My understanding is that you have an Employee object, and you want to get the manager name.  "Table1" is not a very descriptive name, so it is confusing the issue.  I would think that you need to look up FuncMgrID, and get back a value for FuncMgrName


 EmployeeList.ForEach(Function(x) x.FuncMgrName = GetManagerID(x.FuncMgrId))

Open in new window


where EmployeeList is a list of Employee objects, and not just the IDs.
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36573703
Sorry for the confusion.  Table1 is an employee details table that gets data from an SSIS package. I need to save more data to it. The format of the function call the functional manager name is different. It allows me to pass in an employeeid and return the manager name.
I just have to look up the employee id, I pass the id as a parameter to the other system, then save the value to table1.
This complete solution generates an automatic email.  I have all components working.  I just have to save the value to table one.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36573955
Well, you have given me information, and I have tried to provide a solution, but it seems that it still doesn't meet your requirements, so I am at a loss as to how to help you.
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36574039
You helpd me greatly just by helping me iterate through the table and pull each employee id.
For the last piece, I just need to know how to save a string result to a table.  Like if a user inputs data into a textbox.  
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36574231
"save a string result" to a different table, or the same Table1?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Annette Wilson, MSIS
ID: 36574348
Same table.. Table1
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36574375
Then, I would suggest something like this:


Dim EmployeeList = dataContext.Table1.ToList()

EmployeeList.ForEach(Function(x) x.FuncMgrName = GetManagerID(x.EmployeeID))

dataContext.SaveChanges()

Open in new window

0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36574456
Thanks Ill giive this a try and get back to you... have to get to the office now.  I really apreciate yor help.
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36576510
I almost got it.  

I am going to take out the loop from here and call this function in code... to do this, I have to change this from list and just return the employeeID because I am getting an invalid cast:

Public Function GetEmployeeID() As System.Linq.IQueryable(Of Table1)
        Dim dataContext = New MyEntities()
        Dim EmpManName As String = " "
        Dim EmployeeIdList = (From n In dataContext.Table1 Select n.EmployeeID).ToList
       
        Return EmployeeIdList

    End Function


Here's a part of what I have to process in code:
Dim repository As New MyDataRepository
        repository.GetEmployeeID()
        For Each n In repository.GetEmployeeID

            Dim strQry As String = "SELECT HRMangerName"
            strQry += "FROM amEmplDept WHERE IDNo = " & n.EmployeeID
            Dim intErr As Integer =MyAPI.QueryGet(hQry, strQry)
            If (intErr <> 0) Then
                Console.WriteLine("Error Obtaining Employee HR Manager Name")
                Return
            Else
                HRMgrName = MyAPI.FieldStrValue(hQry, 0)
                repository.GetTalentManID(n.EmployeeID, HRMgrName)
                repository.SaveChanges()

            End If
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36576532
If you stick to that form, you need this:

(From n In dataContext.Table1 Select n).ToList
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36576585
I'm sorry.  I don't understand.  where would this go?  Tolist is giving me errors as I am passing string values.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 36576678
Dim EmployeeIdList = (From n In dataContext.Table1 Select n).ToList
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36576749
I see.  I took away the IQuerable from the GetEmployeeID function.  Now I get an error:

Public member 'EmployeeID' on type 'String' not found.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36576803
What does repository.GetEmployeeID() return?
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 36576808
I seee!  

How would I change the following?

repository.GetTalentManID(n.EmployeeID, HRMgrName)
                repository.SaveChanges()
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36576901
In order to write changes to an entity, you need a reference to that entity object, with a property that you can set, in order to update the entity value, and then call SaveChanges on the data context.

Something like this:

n.HRMgrName = repository.GetTalentManID(n.EmployeeID)

Open in new window

0
 

Author Closing Comment

by:Annette Wilson, MSIS
ID: 36577505
Thank you so much TheLearnedOne!  
Thanks for haning in there with me to the end.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now