我可以使用同一个表的两个不同的列值,并使用LINQ连接另一个表中的不同的一个列值吗?

时间:2016-08-19 21:58:29

标签: c# sql sql-server linq datagridview

我有一个包含许多表的样本货运公司数据库。其中一些是"发货","发货详细信息"和"客户"表。 "顾客"表保存公司客户的信息; "出货"表格保存公司每批货物的一般信息和货件ID,以及" ShipmentDetails"保留这些货物的具体信息,如体积,重量,成本,距离等。

我的应用程序表单之一,用户可以使用他们自己的和他们的接收者个人识别号码查询他们的货物信息(& status)。我编写了以下查询来实现它。

> var q = from shipments in model.Shipments
>                     join shipmentdetails in model.ShipmentDetails on shipments.ID equals shipmentdetails.ShipmentID
>                     where shipments.SenderTCID == maskedTextBox1.Text && shipments.ReceiverTCID == maskedTextBox2.Text
>                     select new
>                     {
>                         shipments.ID,
>                         shipments.Status,
>                         shipments.SenderTCID,
>                         shipments.ReceiverTCID,
>                         shipments.ReceivingDate,
>                         shipments.DeliveryDate,
>                         shipmentdetails.Weight,
>                         shipmentdetails.Volume,
>                         shipmentdetails.Type,
>                         shipmentdetails.Cost,
>                         shipmentdetails.Distance,
>                     };
>             dataGridView1.DataSource = q.ToList();

后来,我想根据查询货物信息中的SenderID和ReceiverID值添加我的datagridview这些客户名称。我希望显示为" '150'(ShipmentID) - 'Delivered'(Status) - 123456789 (SenderID) - Michael (Sender Name) - 123456781(ReceiverID) - John (Receiver Name) - .... ".

我可以在不更改表格的情况下以这种方式更改代码吗? (不将SenderName和ReceiverName值添加到"货件"表)。

2 个答案:

答案 0 :(得分:0)

我不确定你在哪里存储发件人/收件人名称所以我确实创建了客户表(类对象),我放了两个成员,其中一个是接收者,另一个是发送者。

接下来,为了使您的热门查询保持原样,您可以在选择新的匿名类型{...}中添加成员声明,​​然后在相应的属性(接收者名称,发件人姓名)中,您可以执行子操作 - 选择根据id检索名称。由于var将保存IEnumerable,你可以执行q1.First()来检索第一个元素。

using System;
using System.Linq;

namespace TestConsole
{
   public class StackOverflow
   {

      public void test()
      {
         int senderid = 123456789;
         int receiverid = 123456781;

         Custommer[] cust = new Custommer[]
         {
            new Custommer()
            {
                ID = 123456789,
                Name = "John_Sender"
            },
            new Custommer()
            {
                ID = 123456781,
                Name = "Bob_Receiver"
            }
         };

         Shipments[] shipments = new Shipments[]
         {
            new Shipments()
            {
                ID = 150,
                SenderID = 123456789,
                ReceiverID = 123456781,
                ReceivingDate = new DateTime(2016, 09, 01),
                DeliveryDate = new DateTime(2016, 09, 02),
                Status = "Delivered"
            },
         };

         ShipmentDetail[] shipmentdetails = new ShipmentDetail[]
         {
            new ShipmentDetail()
            {
                ShipmentID = 150,
                Weight = 25.00,
                Volume = 25.00,
                Type = "Whatever",
                Cost = 25.00,
                Distance = 25.00
            },
         };

        var q1 = from _shipment in shipments
                join _shipmentdetail in shipmentdetails on _shipment.ID equals _shipmentdetail.ShipmentID
                where _shipment.SenderID == senderid && _shipment.ReceiverID == receiverid
                select new
                {
                    ShipmentID = _shipment.ID,
                    Status = _shipment.Status,
                    SenderID = _shipment.SenderID,
                    SenderName = cust.Where(s => s.ID == _shipment.SenderID).Select(s => s.Name).FirstOrDefault(),
                    ReceiverID = _shipment.ReceiverID,
                    ReceiverName = cust.Where(s => s.ID == _shipment.ReceiverID).Select(s => s.Name).FirstOrDefault(),
                };
        var q2 = q1.First();

        Console.WriteLine
        (
            "Shipment Id  = " + q2.ShipmentID.ToString() + "\n" +
            "Status = " + q2.Status + "\n" +
            "Sender ID = " + q2.SenderID + "\n" +
            "SenderName = " + q2.SenderName + "\n" +
            "Receiver ID = " + q2.ReceiverID + "\n" +
            "Receiver Name = " + q2.ReceiverName
        );
    }

    public class Custommer
    {
        public int ID;
        public string Name;
    }

    public class Shipments
    {
        public int ID;
        public string Status;
        public int SenderID;
        public int ReceiverID;
        public DateTime ReceivingDate;
        public DateTime DeliveryDate;
    }

    public class ShipmentDetail
    {
        public int ShipmentID;
        public double Weight;
        public double Volume;
        public string Type;
        public double Cost;
        public double Distance;
    }
  }
}

Console result

答案 1 :(得分:-1)

    **Yes You Can do this by Add two more field in your Shipments model.
    SenderName ,ReceiverName**

    if Your Model.Shipments have not SenderName and ReceiverName in Database,
    Than You Need to Create Class for Shipment Reference 
    Ex 


    public class ShipmentView
{
public int Id{get;set;}
public bool Status{get;set;}
public double Cost{get;set;}
public string Type{get;set;}
public string ReceiverName{get;set;}
public string SenderName{get;set;}
}




        select new ShipmentView()
        >                     {
        >                        Id = shipments.ID,
        >                        Status = shipments.Status,
        >                        SenderTCID = shipments.SenderTCID,
                                 SenderName = (from sendername in model.Sender where sendername.SenderId == shipments.SenderId select sendername).single().SenderName, 
        ReceiverName = (from receivername in model.Receiver where receivername.ReceiverId == shipments.ReceiverId select receivername).single().ReceieverName, 
        >                         shipments.ReceiverTCID,
        >                         shipments.ReceivingDate,
        >                         shipments.DeliveryDate,
        >                         shipmentdetails.Weight,
        >                         shipmentdetails.Volume,
        >                         shipmentdetails.Type,
        >                         shipmentdetails.Cost,
        >                         shipmentdetails.Distance,
        >                     };