LINQ代码结合两个数据库表?

时间:2015-10-05 13:04:32

标签: c# mysql entity-framework linq razor

所以我有一个表,我在一个网站上显示,通过一些LINQ代码从数据库获取所有信息。表" db.Log" 包含一些内容和一个外键 customerId ,该链接与 ID 相关联数据库名为 dbo.customers (它只有Id和customerName)我希望LINQ代码能够将这两者结合起来,这样我就可以在表格中填写客户名称除非你知道所有内容,否则不是真正说出任何内容的ID。以下是当前代码:

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; }

    }
}

我对LINQ代码非常陌生,请原谅我,任何帮助都将不胜感激。

提前致谢。

编辑: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; }
    }
}

我认为(在与朋友交谈后)需要这个模型 - 可能完全错误。

user1666620建议后的代码

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; }

    }
}

错误:

  

指定的LINQ表达式包含对查询的引用   与不同的背景相关联。

2 个答案:

答案 0 :(得分:1)

这应该适合你:

var message = (from log in db.Log
               join customers in dbo.customers on log.customerid equals customers.Id
               select log); 

在上面,没有必要明确使用where子句,因为它在连接中处理。

如果您只想返回特定列,可以执行以下操作:

var message = (from log in db.Log
               join customers in dbo.customers on log.customerid equals customers.Id
               select new { customerId = customers.Id, logId = log.id }); 

如果您想在两个上下文之间进行加入,请查看此解决方法:Simulating Cross Context Joins--LINQ/C#

答案 1 :(得分:1)

<强>控制器:

message = db.GetLogs();

int pageSize = 10;
int pageNumber = (page ?? 1);
var logs = message.OrderByDescending(i => i.timeStamp).ToPagedList(pageNumber, pageSize);

foreach (var log in logs)
    log.Name = Customer.Where(a => a.Value == log.customerId.ToString()).FirstOrDefault().Text;
                return PartialView("_LogPartialLayout", logs);

<强>型号:

using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Collections.Generic;
using System.Web.Mvc;

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; }
        [NotMapped]
        public string Name { get; set; }
    }

    public class LogDBContext:DbContext
    {
        public LogDBContext() : base("MySqlConnection")
        {

        }

        public DbSet <LogModel> Log { get; set; }

        public IQueryable<LogModel> GetLogs()
        {
            return from log in Log
                   select log;
        }
    }
}