试图将两个表linq连接到Sql

时间:2014-06-18 10:22:09

标签: c# sql linq

我有以下表格:

Table Name: console_details               Table Name: console_features
-------------------                         -----------------
| console_id      |                         |       id      |
-------------------                         -----------------
|console_name     |                         |     bluray    |
-------------------                         -----------------
|console_type     |                         |   harddrive   |
-------------------                         -----------------
|console_startdate|
-------------------

console_id字段和id字段共享相同的数据公共字段,我想将这两个表从linq连接到sql查询,并获得控制台的硬盘大小。我无法在stackoverflow上找到与此类似的其他问题。那有什么建议吗?

这是我的linq代码:

public IEnumerable<console_details> GetConsole(DateTime startDate, DateTime endDate)
{
    DataClasses1DataContext mydatabase = new DataClasses1DataContext();
    var results = from data in mydatabase.console_details
                  where data.console_startdate >= startDate &&
                        endDate >= data.console_startdate
                  select data;

    return results;
}

2 个答案:

答案 0 :(得分:2)

你可以尝试这个:

DataClasses1DataContext mydatabase = new DataClasses1DataContext();
var results = from cd in mydatabase.console_details   
              join cf in mydatabase.console_features
              on cd.console_id equals cf.id
              where cd.console_startdate >= startDate && 
                    endDate >= cd.console_startdate
              select new { Name = cd.console_name, HardDriveSize = cf.harddrive };

使用上述查询,您将获得具有两个属性的匿名类型的对象序列

  • Name,这是console_name。
  • HardDriveSize,这是硬盘的大小。

<强>更新

首先,我们必须声明一个具有我们需要的所有属性的类型。我将这种类型称为ConsoleView。您可以随意调用它,但您必须在以下代码中进行相应的更改。

public class ConsoleView
{
    // console_id
    public int Id { get; set; }

    // console_name
    public string Name { get; set; }

    // console_type
    public string ConsoleType {get; set; }

    // console_startdate
    public DateTime StartDate { get; set; }

    // harddrive
    public int HardDrive { get; set; }
}

假设 harddrive的类型是int,console_type的类型是字符串。 如果这不是真的,你必须相应地改变它们。

然后您的方法应更改为以下方法:

public IEnumerable<ConsoleView> GetConsole(DateTime startDate, DateTime endDate)
{
    DataClasses1DataContext mydatabase = new DataClasses1DataContext();

    IEnumerable<ConsoleView> results = from cd in mydatabase.console_details   
                                       join cf in mydatabase.console_features
                                       on cd.console_id equals cf.id
                                       where cd.console_startdate >= startDate && 
                                       endDate >= cd.console_startdate
                                       select new ConsoleView
                                       {
                                           Id = cd.console_id, 
                                           Name = cd.console_name, 
                                           ConsoleType = cd.console_type,
                                           StartDate = cd.console_startdate,
                                           HardDriveSize = cf.harddrive 
                                       };

    return results;
}

更新#2

根据您在评论中发布的错误我理解,您的数据上下文存在问题。因此,如果构建了DataClasses1DataContext用于访问console_details并且已经构建了DataClasses2DataContext来访问console_features,那么您必须将方法中的代码更改为以下代码:

public IEnumerable<ConsoleView> GetConsole(DateTime startDate, DateTime endDate)
{
    DataClasses1DataContext mydatabase1 = new DataClasses1DataContext();
    DataClasses2DataContext mydatabase2 = new DataClasses2DataContext();

    IEnumerable<ConsoleView> results = from cd in mydatabase1.console_details   
                                       join cf in mydatabase2.console_features
                                       on cd.console_id equals cf.id
                                       where cd.console_startdate >= startDate && 
                                       endDate >= cd.console_startdate
                                       select new ConsoleView
                                       {
                                           Id = cd.console_id, 
                                           Name = cd.console_name, 
                                           ConsoleType = cd.console_type,
                                           StartDate = cd.console_startdate,
                                           HardDriveSize = cf.harddrive 
                                       };

    return results;
}

答案 1 :(得分:1)

为简单起见,我忽略了查询中的startdate和enddate变量。 因为我没有你的数据并且你没有提供抽样数据,所以我使用Linq-to-Objects而不是Linq-to-SQL创建了一个测试用例:

有两个代表你的表的类:

public class console_details
{
    public int Id { get; set; }
    public string console_name { get; set; }
    public string console_type { get; set; }
}

public class console_features
{
    public int Id { get; set; }
    public double blueray { get; set; }
    public double Harddrive { get; set; }
}

示例数据如下所示:

var consoleList = new List<console_details>()
{
    new console_details() {Id = 1, console_name = "Ps4", console_type ="Next-Gen"},
    new console_details() {Id = 2, console_name = "XB1", console_type ="Next-Gen"}
};

var featuresList = new List<console_features>()
{
    new console_features() {Id = 1, blueray = 23.9, Harddrive = 100},
    new console_features() {Id = 2, blueray = 30, Harddrive = 150}
};

最后但并非最不重要的是,我创建了一个ViewModel类来强类型化结果:

public class ConsoleViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double HardDrive { get; set; }
}

使用此查询和输出如下所示:

var joinedResults = from con in consoleList
                    join feat in featuresList on con.Id equals feat.Id
                    select new ConsoleViewModel
                    {
                        Id = con.Id,
                        Name = con.console_name,
                        HardDrive = feat.Harddrive
                    };

enter image description here

使用此方法,您的方法应类似于:

public IEnumerable<ConsoleViewModel> GetConsole(DateTime startDate, DateTime endDate)
{
    DataClasses1DataContext mydatabase = new DataClasses1DataContext();
    var results = from data in mydatabase.console_details
                  join feat in mydatabase.console features on data.Id equals feat.Id
                  where data.console_startdate >= startDate
                        && endDate >= data.console_startdate
                  select new ConsoleViewModel
                    {
                        Id = con.Id,
                        Name = con.console_name,
                        HardDrive = feat.Harddrive
                    };
    return results;
}