私は、あるテーブルから別のテーブルの値と一致する値のコレクションを見つける方法について質問します。ここで2つのテーブルから一致する値を見つける方法はありますか?
は私のコードスニペットです:ここで
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からすべての一致行を取得したいですテーブル番号がに設定されているテーブルと同じ値のテーブルと一致するフィールドを比較しますが、ロジックを設定する方法がわかりません。
私は、上記のSQLクエリで照合/照合したいパラメータと一致する値を表Bから返すだけです。正しい値を返すために上記のコードを改善するにはどうすればよいですか?
EDIT:
Identifier: 123
(のみテーブルAで:識別子123有するコード、HouseNo、姓をつかむ)
その後の値で表Aからのものと一致ここ
は、いくつかのモックデータであります表Bここで、値は以下のサンプル値と同じです。
Code = 456
HouseNo = 34
BusinessName = 'Bar, Foo'
VCode = 'E4T' (passed in to method)
Return TableB上記の情報と一致する行。
私は内部結合ではなく、あなたが左に参加したいと思うかなり確信しています。そうすれば、表Bに一致するものがなくても表Aをすべて返すことができます。 – Greg
サンプルデータと希望の結果を入力してください。 –
テーブルBの結果が一致する場合にのみ結果を返すことができます。それ以外の場合は何も返しません。 – Euridice01