I have two separate models in my MVC project
[Table("Table1")]
public class Companies : IEnumerable
{
[Key]
public double ID { get; set; }
public System.Guid Key { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public string Industry { get; set; }
public string Rank{ get; set; }
public IEnumerator GetEnumerator()
{
yield return this.ID;
yield return this.Key;
yield return this.Name;
yield return this.Company;
yield return this.Industry;
yield return this.Rank;
}
}
and
[Table("Table2")]
public class Registration : IEnumerable
{
[Key]
public System.Guid Key { get; set; }
public string FieldValue{ get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public IEnumerator GetEnumerator()
{
yield return this.Key;
yield return this.FieldValue;
yield return this.StartDate;
yield return this.EndDate;
}
}
I need to display a combination of data in my view:
SELECT t2.FieldValue, t1.Name, t1.Company, t1.Industry, t1.Rank
FROM Table1 as t1 INNER JOIN Table2 as t2 ON t1.Key=t2.Key
I created the following Context class:
public class TablesContext: DbContext
{
public DbSet<Companies> companies { get; set; }
public DbSet<Registration> registration { get; set; }
}
Now I can pass the data for companies from the controller to the view using the following code:
private TablesContext tc = new TablesContext();
var comps = (from c in tc.companies
join r in tc.registry
on c.Key equals r.Key
select new { FieldValue=r.FieldValue, Name=c.Name, Company=c.Company,Industry=c.Industry, Rank=c.Rank});
return View(comps);
Now I am a bit stuck trying to make TablesContext class enumerable and loop through the result set in the View. Can anyone point me in the right direction?