0

I have 2 classes SiteConfig, SiteConfigView. One is tightly coupled with my EF and one class to expose it to View models. Both the classes holds to a collection of type 'Brands'

I struck at writing a linq query to fetch the records from db to view model.

As I am exposing a different class to view model, I have to get the records of type 'SiteConfigView'. So I am writing a linq query but I am bit confused how to get the collection from SiteConfig to SiteConfigView.

There are my classes

public partial class SiteConfig
{
    public SiteConfig()
    {
        this.SiteBrands = new HashSet<SiteBrand>();
    }

    public int IdSiteConfig { get; set; }
    public string Name { get; set; }
    public byte[] SiteLogo { get; set; }
    public string Brands { get; set; }
    public string LinkColour { get; set; }

    public virtual ICollection<SiteBrand> SiteBrands { get; set; }
}

public class SiteConfigView
{
    public SiteConfigView()
    {

    }

    public int IdSiteConfig { get; set; }
    public string Name { get; set; }
    public byte[] SiteLogo { get; set; }
    public string Brands { get; set; }
    public string LinkColour { get; set; }

    public IEnumerable<SiteBrandView> SiteBrands { get; set; }
}

And this is the query I am trying

var db = new SampleMVCEntities();
        IQueryable<SiteConfig> test = db.SiteConfigs.Select(a => new SiteConfigView{Name = a.Name,LinkColour = a.LinkColour,SiteLogo = a.SiteLogo});

Can comebody guide me how to get the collection from SiteConfig to SiteConfigView.

Thanks

Jorge
  • 17,896
  • 19
  • 80
  • 126
Naresh
  • 2,667
  • 13
  • 44
  • 69

1 Answers1

2

You're going on the right direction tried like this

var siteConfigs = db.SiteConfigs.AsEnumerable().Select(a => new SiteConfigView()
                 {
                     Name = a.Name,
                     LinkColour = a.LinkColour,
                     SiteLogo = a.SiteLogo,
                     SiteBrands = a.SiteBrands.AsEnumerable().Select(a => new SiteBrandView()
                     {
                          //Do the projection
                     }).ToList()
                 }).ToList();
Jorge
  • 17,896
  • 19
  • 80
  • 126
  • thanks for reply. But above query does not include the collection SiteBrand. – Naresh Aug 10 '12 at 14:22
  • check my updated answer, sorry but you need to give the definition of the `SiteBrandView` model to help you to do the projection – Jorge Aug 10 '12 at 14:23
  • thanks John. Do we need to call AsEnumerable before calling Select on the SiteConfigs. – Naresh Aug 10 '12 at 14:30
  • The reason to called `AsEnumerable` Interface, it's for the Collection of elements that you're receiving from the DBcontext are IQuerable, and to do the projection `(Convert into a another class in your case a View model)` you need to implement's this interface – Jorge Aug 10 '12 at 14:32
  • I didn't get your point implementing the interface IQuerable. I am able to call .Select directly on SiteConfigs with out calling .AsEnumarable(). Anyhow I will try both the things. And it would be great if you can explain the difference. Thanks. – Naresh Aug 10 '12 at 14:37
  • Maybe this question could help http://stackoverflow.com/questions/252785/what-is-the-difference-between-iqueryablet-and-ienumerablet – Jorge Aug 10 '12 at 14:45