简体   繁体   English

LINQ JOIN两个表并查看两个列

[英]LINQ JOIN two tables and viewing both columns

I Have two tables 我有两张桌子

tblPhases tblPhases

And

tblTruck 卡车

I JOINED these two but I can't seem to use both columns on both tables. 我加入了这两个,但似乎无法在两个表上同时使用两个列。

Phase.cs Phase.cs

public class Phases
{
    public int Id { get; set; }

    public int TruckId { get; set; }
    public virtual RTrucks Truck { get; set; }

    public string Checkin { get; set; }

    public string ChassisPrep { get; set; }

    public string Floor { get; set; }

    public string Body { get; set; }

    public string Paint { get; set; }

    public string Finished { get; set; }

}

RTruck.cs RTruck.cs

public class RTrucks
{
    public int Id { get; set; }

    public string ChassisManufacturer { get; set; }

    public string ChassisModel { get; set; }
}

Service.cs Service.cs

[OperationContract]
    List<Phases> GetPhasesbyTruck(int TruckId);

Service.svc.cs Service.svc.cs

public List<Phases> GetPhasesbyTruck(int TruckId)
    {
        TruckDb db = new TruckDb();

        List<Phases> r = new List<Phases>();

        var join = from p in db.RTrucks
                   join t in db.Phases
                   on p.Id
                   equals t.TruckId
                   where t.Id == TruckId
                   select p;


        foreach (var item in join)
        {
            r.Add(new Phases()
            {
   //this is not the actual coding but just a demonstration of what is 
   //actually happening                                                                                         
   //    Phases item like Id = tblTruck item like truckId        
            });
        }
        return r;

    }

I cannot call the columns from the tblPhases table only from the tbltrucks table, and also how can I use the Phases.cs and RTruck.cs together in my foreach so I can access both class items? 我不能仅从tbltrucks表中调用tblPhases表中的列,而且如何在我的foreach中一起使用Phases.cs和RTruck.cs,以便可以访问这两个类项?

First of all, following code 首先,下面的代码

var join = from p in db.RTrucks
           join t in db.Phases
           on p.Id equals t.TruckId
           select p;

is enough to make a join between two tables. 足以在两个表之间建立联接。 you do not need to add an extra where clause to it. 您无需为其添加额外的where子句。 Secondly, you can either select properties from both tables as mentioned by Arianit in his answer or you can just select Phases as in above query and then you can access Truck through navigation properties like 其次,你可以从在他的回答中提到的Arianit或者你可以选择两个表要么选择属性Phases如在上面的查询,然后你可以访问Truck通过像导航性能

foreach (var p in join){
    int truckId = p.Truck.Id;
    string chesis = p.Truck.ChassisManufacturer;
    string model = p.Truck.ChassisModel;
}

The benefit of second approach is that you don't need a new class to populate properties from both tables. 第二种方法的好处是您不需要新的类即可从两个表中填充属性。 you can access all information through navigation properties. 您可以通过导航属性访问所有信息。

try this: 尝试这个:

var join = from p in db.RTrucks
           join t in db.Phases
           on p.Id equals t.TruckId
           select new {TruckName = p.TruckName, //depends from your table column name
           PhasesName = t.PhaseName
           };

foreach (var item in join)
{
     Console.WriteLine(item.TruckName + "\t" + item.PhaseName);
 }

Remove the where from your linq 从您的linq中删除where

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM