So I have a table that I am showing on a website that gets all the information from a database via some LINQ code. The table "db.Log" contains a few things and a foreign key customerId which is linked to the Id in a database called dbo.customers (it has Id and customerName only) and I would like the LINQ code to combine those two so I can fill the table with the names of the customers instead of an ID which doesn't really say anything unless you know them all. Below is the current code:
LogModelsController.cs
message = (from log
in db.Log
select log);
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(message.OrderByDescending(i => i.timeStamp).ToPagedList(pageNumber, pageSize));
LogModel.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ASDMVC.Models
{
[Table("Log")]
public class LogModel
{
[Key]
public long id { get; set; }
public string message { get; set; }
public DateTime timeStamp { get; set; }
public string level { get; set; }
public int customerId { get; set; }
}
public class LogDBContext:DbContext
{
public LogDBContext() : base("MySqlConnection")
{
}
public DbSet <LogModel> Log { get; set; }
}
}
I'm very new to LINQ code so excuse me for that, any help would be appreciated.
Thanks in advance.
Edit: NewCustomerModel.cs
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Web.Mvc;
namespace ASDMVC.Models
{
[Table("Customers")]
public class NewCustomerModel
{
[Key]
public int Id { get; set; }
[Display(Name = "Customer Id")]
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
public class CustomerDBContext : DbContext
{
public CustomerDBContext() : base("MySqlConnection")
{
}
public DbSet<NewCustomerModel> Customers { get; set; }
}
}
I think (after speaking to a friend) that this model is needed - might be completely wrong.
Code after user1666620 suggestion
LogModelsController.cs
message = (from log in db.Log join customers in dbo.Customers on log.customerId equals customers.Id select log);
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(message.OrderByDescending(i => i.timeStamp).ToPagedList(pageNumber, pageSize));
LogModel.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ASDMVC.Models
{
[Table("Log")]
public class LogModel
{
[Key]
public long id { get; set; }
public string message { get; set; }
public DateTime timeStamp { get; set; }
public string level { get; set; }
public int customerId { get; set; }
public int customerName { get; set; }
}
public class LogDBContext:DbContext
{
public LogDBContext() : base("MySqlConnection")
{
}
public DbSet <LogModel> Log { get; set; }
}
}
With the error:
The specified LINQ expression contains references to queries that are associated with different contexts.