Print Page | Close Window

Selecting certain properties of a DF/EF object.

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2245
Printed Date: 20-Oct-2025 at 5:44am


Topic: Selecting certain properties of a DF/EF object.
Posted By: geo
Subject: Selecting certain properties of a DF/EF object.
Date Posted: 19-Oct-2010 at 5:56pm
How can I select certain properties to return without creating a new anonymous object? My goal is to use the Employee object I would normally get back but only fill certain properties in an effort to reduce the data going across my WCF service. Is this possible? With this code I get this error where I set Employee=item within my foreach loop.

Cannot implicitly convert type 'AnonymousType#1' to 'AmberStream.Model.Payroll.Employee'

            var query = _entityManager
                .Employees
                .AddIncludePaths("Person")
                .Where(col => col.Employee_ID == id)
                .Select(e => new { e.Employee_ID, e.Person.FirstName, e.Person.LastName })
                .ExecuteAsync(op => {
                    if (op.HasError)
                        throw op.Error;

                    foreach (var item in op.Results)
                    {
                        Employee = item; 
                    }
                    
                    // debugging log
                    ObjectDumper.Write(Employee);
                    ObjectDumper.Write(Employee.Person.FirstName);
                    ObjectDumper.Write(Employee.Person.LastName);
                });



Replies:
Posted By: sbelini
Date Posted: 20-Oct-2010 at 10:18am
geo,
 
you can try:
  .
  .
  .
  .Select(e => new { id = e.Employee_ID, fname = e.Person.FirstName, lname = e.Person.LastName })
  .
  .
  .
  foreach (var item in op.Results){
    Employee.Employee_ID = item.id;
    Employee.Person.FirstName = item.fname;
    Employee.Person.LastName = item.lname;
  }
 
 



Print Page | Close Window