【问题标题】:Query - Full outer join 2 different tables in a Dataset - LINQ C#查询 - 数据集中的完全外连接 2 个不同的表 - LINQ C#
【发布时间】:2017-12-19 12:44:35
【问题描述】:

我想从两个不同的表进行连接(它们之间没有连接):

Parkinglot(parkingLotID、addressParkingLot、statusParkingLot)

PublicParking(publicParkingID、addressPublicParking、statusParking)。

我想编写一个查询,返回所有可用的停车位 - 基于他们的状态(停车场和公共停车场)。

我读到我需要进行完全外连接(制作一张大表),然后才能编写查询。

我需要在 LINQ 中编写查询。

关于这个查询和完整的外部连接(如果正确的话),我真的需要你的帮助

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {

        IList<parkingLot> parkingLot=new List <parkingLot>(){

        new parkingLot {parkingLotID=1, addressParkingLot="bograshov 22",statusParkingLot=true},
        new parkingLot {parkingLotID=2, addressParkingLot="bograshov 10",statusParkingLot=false},
        new parkingLot {parkingLotID=3, addressParkingLot="bograshov 28",statusParkingLot=true},
    };



    IList<publicParking> PublicParking=new List <publicParking>(){

        new publicParking {publicParkingID=101, addressPublicParking= "bograshov 23",statusParking=true},
        new publicParking {publicParkingID=102, addressPublicParking= "bograshov 21",statusParking=true},
        new publicParking {publicParkingID=103, addressPublicParking= "bograshov 18",statusParking=false},
    };


  (from lot in parkingLot
    where lot.statusParkingLot == true
    select lot).Union(from pub in PublicParking
    where pub.statusParking==true
    select pub);


   }
}

public class publicParking 
{
 public int publicParkingID { get; set; }
 public string addressPublicParking { get; set; }
    public bool statusParking { get; set; }

}


public class parkingLot 
{
  public int parkingLotID { get; set; }
  public string addressParkingLot { get; set; }
  public bool statusParkingLot { get; set; }

 }

TNX!

更新

我写了查询,但它有问题:

New problem

【问题讨论】:

  • 你能发布示例代码吗?
  • 什么意思?
  • 您已经尝试过的示例代码。例如您所指的两个查询连接。
  • from lot in ParkingLots from pub in PublicParkings where lot.StatusParkingLot == false && pub.StatusParking==false select lot,pub
  • 请将您的代码添加到您的问题中。评论与您的​​问题无关,它是根据您添加到线程中的内容来讨论您的问题。

标签: c# sql asp.net linq datatables


【解决方案1】:

您可以使用Union 连接两个没有公共字段的表。您的方案的 LINQ 查询将如下所示。

 (from lot in ParkingLots
 where lot.StatusParkingLot == true
 select lot).Union( from pub in PublicParkings
 where pub.StatusParking==true
 select pub);

希望这行得通!

【讨论】:

  • 哈比卜·乌尔·拉赫曼。我非常感谢!还是有问题。我已经在代码编辑器和 LINQPad 中尝试过。错误图片在原图上
  • 两个表中的运算符数量是否相等?
  • 我需要放更多的东西吗?使用系统;使用 System.Linq;使用 System.Collections.Generic;
  • 这是完整的运营商: public class publicParking { public int publicParkingID { get;放; } 公共字符串地址PublicParking { 获取;放; } 公共双纬度 { 得到;放; } 公共双经度 { 得到;放; } 公共布尔状态停车 { 获取;放; } 公共 int PricePerHourpublicParking { 获取;放; } 公共双 PricePerMinutepublicParking { 获取;放; } 公共布尔 isOrdered { 获取;放; } }
  • 公共类parkingLot { public int parkLotID { get;放; } 公共字符串地址ParkingLot { 获取;放; } 公共双纬ParkingLot { 得到;放; } 公共双 longtitudeParkingLot { 得到;放; } 公共 int maxSpaces { 获取;放; } 公共 int 占用空间 { 获取;放; } 公共 int freeSpaces { 获取;放; } 公共 int PricePerHour { 获取;放; } 公共双 PricePerMinute { 获取;放; } public bool statusParkingLot { get;放; } }
猜你喜欢
  • 2014-09-14
  • 2018-03-04
  • 2018-05-24
  • 2014-08-07
  • 2017-01-19
  • 2015-11-04
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
相关资源
最近更新 更多