如何从两个表中找到匹配的值?

时间:2017-12-26 19:35:50

标签: c# sql sqlite

我有一个关于如何从一个表中查找与另一个表中的某些值匹配的值集合的问题。

这是我的代码段:

public class DBTableA
{
    [PrimaryKey, AutoIncrement]
    public int DBTableAKey { get; set; }
    [NotNull]
    [Indexed]
    public Int64 Identifier { get; set; }
    [NotNull]
    public int Code { get; set; }
    [Indexed]
    public int? HouseNo { get; set; }
    public string FirstName { get; set; }
}

public class DBTableB
{
    [PrimaryKey, AutoIncrement]
    public int DBTableBKey { get; set; }
    [NotNull]
    [Indexed]
    public string BoroCode { get; set; }
    [NotNull]
    [Indexed]
    public int Code { get; set; }
    [NotNull]
    [Indexed]
    public string HouseNo { get; set; }
    [NotNull]
    public DateTime Date { get; set; }
    [NotNull]
    public string BusinessName { get; set; }
    [NotNull]
    [Indexed]
    public string VCode { get; set; }
}

以下是实体:

Identifier: 123

基本上,使用TableA中的Identifier,我想得到表A中的所有匹配行,其中Identifier等于传入的数。然后,进行比较,其中来自该tableA集的select字段与TableB集中的相同值匹配 但我不确定如何设置逻辑,我在上面添加了我的尝试。

我的意思是我只想从表B中返回与我想在上面的sql查询中检查/匹配的参数相匹配的值(如果有的话)。如何改进上面的代码以返回正确的值?

编辑:

这是一些模拟数据:

Code = 456 HouseNo = 34 BusinessName = 'Bar, Foo' VCode = 'E4T' (passed in to method) (仅限于表A:抓取代码,HouseNo,具有标识符123的FirstName)

然后将表A中的值与表B中的值进行匹配,其中值与下面的样本值相同:

import win32com.client 

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.Folders['xyz@xyz.com'].Folders['Inbox'].Folders['abc']
messagesReach = inbox.Items
for message in messagesReach:
    if message.Unread==True:
       print(message.body)

返回与上述信息匹配的TableB行。

1 个答案:

答案 0 :(得分:0)

您应该只能使用LINQ来处理此查询:

var ans = (from a in tableARepo
           where a.Identifier == number
           join b in tableBRepo on new { a.Code, HouseNo = a.HouseNo.ToString(), a.FirstName, VCode = vCode } equals new { b.Code, b.HouseNo, FirstName = b.BusinessName, b.VCode }
           select b).ToList();
return ans;