views:

57

answers:

3

Im making this method retrieve records from the Data Base. As you can see i Want to return a List where ITieneID is an Interface defined on my business layer.

AtlasWFM_Entities.Clases.Area implements that interface. It is pretty clear that this is not a good way to accomplishing it (even though it compiles correctly)

public override List<ITieneID> Buscar(ITieneID elementoPatron)
    {
        List<ITieneID> result = new List<ITieneID>();

        var resultado = from a in base.Repository.Context.Areas
                        where a.areaID.Equals(elementoPatron.ID) || a.areaDescripcion.Contains(elementoPatron.Descripcion)
                        select new AtlasWFM_Entities.Clases.Area
                        {
                            ID = a.areaID,
                            Descripcion = a.areaDescripcion,
                            Estado = a.areaEstado,
                        };

        foreach (var r in resultado)
        {
            ITieneID t = new AtlasWFM_Entities.Clases.Area
            {
                ID = r.ID,
                Descripcion = r.Descripcion,
                Estado = r.Estado,
            };
            result.Add(t);
        }
        return result;
    }

Any Ideas how to improve this??

Thanks in advance.

+1  A: 

If Area implements the ITieneID interface just do

  var resultado = from a in base.Repository.Context.Areas
                    where a.areaID.Equals(elementoPatron.ID) || a.areaDescripcion.Contains(elementoPatron.Descripcion)
                    select new AtlasWFM_Entities.Clases.Area
                    {
                        ID = a.areaID,
                        Descripcion = a.areaDescripcion,
                        Estado = a.areaEstado,
                    };


   result  =  resultado.ToList().ConvertAll(x=> x as ITieneID);

   return result;
Nix
@Nix, this one is not working since the ConvertAll is just an extension for LIST types and no for IQueryable.
MRFerocius
did you add .ToList() i just edited the above...
Nix
@Nix, thanks this is compiling now and I hope working too. Thank you pal!
MRFerocius
+1  A: 
public override List<ITieneID> Buscar(ITieneID elementoPatron) 
    { 
        var resultado = from a in base.Repository.Context.Areas 
                        where a.areaID.Equals(elementoPatron.ID) || a.areaDescripcion.Contains(elementoPatron.Descripcion) 
                        select new AtlasWFM_Entities.Clases.Area 
                        { 
                            ID = a.areaID, 
                            Descripcion = a.areaDescripcion, 
                            Estado = a.areaEstado, 
                        }; 
        return new List<ITieneID>(resultado); 
   }

UPDATE: That don't compile, but this should:

        return new List<ITieneID>(resultado.Cast<ITieneID>()); 

In fact, I think you can reduce the whole thing to:

public override List<ITieneID> Buscar(ITieneID elementoPatron) 
    { 
        var resultado = from a in base.Repository.Context.Areas 
                        where a.areaID == elementoPatron.ID || a.areaDescripcion.Contains(elementoPatron.Descripcion) 
                        select a; 
        return new List<ITieneID>(resultado.Cast<ITieneID>()); 
   }

of a little more concise:

public override List<ITieneID> Buscar(ITieneID elementoPatron) 
{ 
        return new List<ITieneID>(base.Repository.Context.Areas
              .Where(a=>a.areaID == elementoPatron.ID || a.areaDescripcion.Contains(elementoPatron.Descripcion))
              .Cast<ITieneID>()); 
}
James Curran
I *suspect* LINQ to SQL won't like the use of the Area class here - I think there are restrictions on what types you can build. I could be wrong though.
Jon Skeet
does that even compile?
Nix
Well John, OP already uses it - only OP gets a group of Area, then uses it to instantiate another group of Area for some criptic reason.
ANeves
This one is not compiling... My problem is that I can´t make a Select new ITieneID (since is an interface) this is why I need to convert the items on another loop or with something else...
MRFerocius
James this is sweet!!!! Compiling nice and pretty concise. Thanks!!!
MRFerocius
+1  A: 

Well, if you need to do this in two "hops" in order to work around any LINQ to SQL limitations, you could use LINQ to SQL and then LINQ to Objects:

var resultado = from a in base.Repository.Context.Areas
                where a.areaID.Equals(elementoPatron.ID) || 
                      a.areaDescripcion.Contains(elementoPatron.Descripcion)
                select new AtlasWFM_Entities.Clases.Area
                {
                    ID = a.areaID,
                    Descripcion = a.areaDescripcion,
                    Estado = a.areaEstado,
                };

return resultado.AsEnumerable() // Do the rest in LINQ to objects
                .Select(r => new AtlasWFM_Entities.Clases.Area
                        {
                            ID = r.ID,
                            Descripcion = r.Descripcion,
                            Estado = r.Estado,
                        })
                .ToList();
Jon Skeet