【发布时间】:2015-10-28 18:29:46
【问题描述】:
在 LINQ 中必须有一种方法可以比较两组结果。这是我现有的代码,它使用 HashSet 在两个单独的查询后进行比较:
public static void AssertDealershipsShareTransactionGatewayCredentialIds(long DealershipLocationId1,
long DealershipLocationId2)
{
using (var sqlDatabase = new SqlDatabaseConnection())
{
var DealershipCredentials1 =
sqlDatabase.Tables.DealershipLocationTransactionGateway
.Where(x => x.DealershipLocationId == DealershipLocationId1)
.Select(x => x.TransactionGatewayCredentialId);
var DealershipCredentials2 =
sqlDatabase.Tables.DealershipLocationTransactionGateway
.Where(x => x.DealershipLocationId == DealershipLocationId2)
.Select(x => x.TransactionGatewayCredentialId);
var doSetsOfCredentialsMatch = new HashSet<int>(DealershipCredentials1).SetEquals(DealershipCredentials2);
Assert.IsTrue(doSetsOfCredentialsMatch,
"The sets of TransactionGatewayCredentialIds belonging to each Dealership did not match");
}
}
想法?谢谢。
【问题讨论】:
-
Intersect LINQ query的可能重复
-
你的方式有什么问题?对我来说看起来很完美。否则,请参阅 stackoverflow.com/questions/33245613/… 和 stackoverflow.com/questions/3669970/…
-
它进行两次数据库调用,返回多条记录,而不仅仅是一个布尔值。
标签: c# database linq intersection