【问题标题】:How can I find matching values from two tables?如何从两个表中找到匹配的值?
【发布时间】:2017-12-26 19:35:50
【问题描述】:

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

这是我的代码 sn-p:

public async Task<List<TableB>> GetTableBResults(string vCode, string number)
{
    var tableARepo = DependencyResolver.Get<IRepository<DBTableA>>();
    var TableBRepo = DependencyResolver.Get<IRepository<DBTableB>>();
    var tableAQuery = string.Format("SELECT * FROM DBTableA WHERE Identifier = '{0}'",
        number);

    List<DBTableA> tableA = await tableARepo.QueryAsync(tableAQuery);


    if (tableA != null)
    {
        //Find all tableB records with info from Identifier
        //And then do a distinct on BusinessName and return those results

        foreach (var item in DBTableA)
        {
            var TableBQuery = String.Format("SELECT *" +
                            "FROM[DBTableB] INNER JOIN DBTableA" +
                            "ON  DBTableB.Code = {0}" +
                            "AND DBTableB.HouseNo = {1}" + 
                            "AND DBTableB.BusinessName = {2}" +
                            "AND DBTableB.VCode = {3}",
                            item.Code, item.HouseNo, item.FirstName, vCode); 

            List<DBTableB> tableB = await TableBRepo.QueryAsync(TableBQuery);

            if (tableBs != null)
            {
                return tableBs.Select(_ => new TableB
                {
                    BoroCode = _.BoroCode,
                    Code = _.Code,
                    HouseNo = _.HouseNo,
                    Date = _.Date,
                    BusinessName = _.BusinessName,
                }).ToList();
            }
            else
            {
                return new List<TableB>();
            }
        }

    }
    return new List<TableB>();
}

以下是实体:

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; }
}

基本上,使用 TableA 中的标识符,我想从表 A 中获取所有匹配行,其中标识符等于传入的数字。然后,进行比较,其中 tableA 集中的选择字段匹配 TableB 集中的相同值 但我不确定如何设置逻辑,我在上面添加了我的尝试。

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

编辑:

这是一些模拟数据:

Identifier: 123(仅在 TableA 中:获取标识符为 123 的 Code、HouseNo、FirstName)

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

Code = 456
HouseNo = 34
BusinessName = 'Bar, Foo' 

VCode = 'E4T' (passed in to method)

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

【问题讨论】:

  • 我很确定你会想要一个左连接,而不是内连接。这样即使表 B 中没有匹配项,您也会返回所有表 A。
  • 请提供样本数据和想要的结果。
  • 如果有匹配项,我只想返回表 B 的结果。否则,不返回任何内容。
  • @PM77-1 我编辑了我的帖子。这有帮助吗?
  • 您的 SQL 看起来不正确。你测试过代码吗?当您编写ON DBTableB.Code = {0} 时,您不是在比较tableA 和tableB 中的字段。如果您已经知道并拥有 TableA 中的项目,那么为什么需要加入。为什么不运行一个简单的选择查询来比较项目中的值?我想你已经结束了。

标签: c# sql sqlite


【解决方案1】:

您应该能够使用 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;

【讨论】:

  • 我尝试了上面的方法,但我得到:找不到源类型'DBTableA.的查询模式的实现。 'Where' 未找到且 'DBTableB' 是一种类型,在给定的上下文中无效
  • 抱歉,您需要 sqlite LINQ 提供程序,并且您必须查询表,而不是类类型。我稍微修改了答案,但我不知道您是否可以针对IRepository 使用LINQ。也许您需要更多 LINQ to SQLite 知识。
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 2016-03-03
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 2020-07-04
  • 1970-01-01
相关资源
最近更新 更多