0
votes

in my wpf mvvm architecture I have 2 tables named department and employee.DepartmentId is the foreign key of Employee table. I am using stored procedure to getting data. The procedure is working fine and getting the details in sql management studio. My Storeed procedure is

Select * from Employee e inner join Department d on e.DepartmentId = d.departmentId

In my mvvm architecture the department name is not getting while selecting the employee details in the ObservableCollection in viewmodel.How can we get the DepartmentName in ObservableCollection in mvvm viewmodel.

My employee model Class is

public  class Employee
   {
public Employee()
       {
           this.Departments = new HashSet<Department>();
       }
       public long DepartmentId;
       public long EmployeeId;
       public string Name;
public virtual ICollection<Department> Departments { get; set; }
}
}

And My ObservableCollection in viewmodel is

public ObservableCollection<Employee> GetEmployeeList()
{
var result = from p in dc.sel_Employee().AsEnumerable()
                       select new 
                       {
                           EmployeeId= p.EmployeeId,
                           Name = p.Name
                       };
return new ObservableCollection<Employee>(result);
}

The error showing is

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

Can anyone help.Thanks in advance.

2

2 Answers

1
votes

Change return type of GetEmployeeList method to IList<object> or ObservableCollection<object> You create anonymous type in linq, but you can't return anonymous type.

Also you can search solutions for return anonymous types from linq

1
votes

Or maybe you should just change

// your prev code
select new 
{
    EmployeeId= p.EmployeeId,
    Name = p.Name
};

to

// your prev code
select new Employee()
{
    EmployeeId= p.EmployeeId,
    Name = p.Name
};

In that solution you don't use anonymous types. But if dc.sel_Employee().AsEnumerable() returns enumerable colection of Employee you should just omit select clause:

public ObservableCollection<Employee> GetEmployeeList()
{
    return new ObservableCollection<Employee>(dc.sel_Employee().AsEnumerable());
}

There is no join clause but I think this is not your real problem. If you use EF (or different ORM) and you have all FK defined correctly then you don't have to worry about joins. The only one thing that I will consider is usage of ObjectQuery<T>.Include Method method.