views:

771

answers:

1

Hi,

I am developing an application with RIA Services, and in my DomainService class, I have all that standard functions generated by the RIA for general CRUD operations. The problem is that I tryed to create my own function that insted of listing all the the 45 columns in the Select statment, would list only 2 (NOM and PRENOM) and also according the parameters received in the clause Where.

The Clause conditions are working perfectly, but the code is still returning all the 45 Columns instead of only the 2 specified. Here is the code:

    public IQueryable<EMPLOYE> GetEMPLOYEs(string strPRENOM, string strNOM)
    {
        IQueryable<EMPLOYE> query = this.Context.EMPLOYEs.AsQueryable();

        //This Doesn't work... all 45 clomuns are returned!!!
        query = from e in this.Context.EMPLOYEs select e;
        query.Select(e => new { e.PRENOM, e.NOM });

        // This Doesn't Work too!!!! Error:Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<AffPoste.Web.EMPLOYE>'. An explicit conversion exists (are you missing a cast?)
        //query = from e in this.Context.EMPLOYEs select new{e.PRENOM, e.NOM};

         // Clause Conditions "Where"
         if (!String.IsNullOrEmpty(strPRENOM)) query = query.Where(e => e.PRENOM.Contains(strPRENOM));
            if (!String.IsNullOrEmpty(strNOM)) query = query.Where(e => e.NOM.Contains(strNOM));

        return query;
    }

Thanks In Advance.

+1  A: 

Escape from

... all 45 clomuns are returned!!!

use Data Transfer Objects

Create a simple DTO class:

public class EmployeDTO
{      
    [Key]
    int ID { get; set; }

    public string Prenom { get; set; }
    public string Nom { get;set; }
}

Create a simple query function in your DomainService:

public IQueryable< EmployeDTO > EmployesDTO(string strPRENOM, string strNOM)
{
    var query = this.Context.EMPLOYEs.AsQueryable();

    if (!String.IsNullOrEmpty(strPRENOM)) query = query
              .Where(e => e.PRENOM.Contains(strPRENOM));
    if (!String.IsNullOrEmpty(strNOM)) query = query
              .Where(e => e.NOM.Contains(strNOM));

   return from emp in query  select ( 
               new EmployeDTO () {
                   ID = emp.ID,
                   Prenom = emp.Prenom, 
                   Nom = emp.Nom
               });             
}

In your domain service EmployeDTO is a regular POCO class and from client side perspective (silverlight) is a RIA entity.

PS: The code was not tested - but I hope you catch the idea.

Good luck.

rlodina