【发布时间】:2020-10-26 20:28:25
【问题描述】:
我有两个表集:
一个实体(值“-1”到“-7”)和一个静态数据(值“-8”到“-11”)。
静态数据表依赖于实体表才能成功验证。我正在尝试检查静态数据表是否存在而实体表不存在,如果存在则将异常附加到异常 StringBuilder。
以下代码完成了这项工作并按预期工作,但是,它令人眼花缭乱,我相信必须有更好的方法来做到这一点。我在 Google 上查看过,但我发现没有一个似乎与这个特定场景有关。
//Check Entities (Company, Trust, Individual) exist to link static data (Address, Email, Bank Account, Phone Number) to, if not add exception
if ((!imports.Tables.Contains("-1") && !imports.Tables.Contains("-2") && !imports.Tables.Contains("-3") &&
!imports.Tables.Contains("-4") && !imports.Tables.Contains("-5") && !imports.Tables.Contains("-6") &&
!imports.Tables.Contains("-7")) && imports.Tables.Contains("-8") && imports.Tables.Contains("-9") &&
imports.Tables.Contains("-10") && imports.Tables.Contains("-11"))
{
exception.Append("No Entity Found: Address, Email, Bank Account & Phone Number require an Entity (Trust, Company, Individual) to be created to pass validation.");
exception.AppendLine();
}
我尝试将值添加到列表并在列表上进行比较,但未成功,以下是我为其中一张表收集的内容:
List<string> staticData = new List<string>{ staticData.Add("-8"); staticData.Add("-9"); staticData.Add("-10"); staticData.Add("-11")}
if((imports.Tables.Contains(staticData) {} // Didn't expect this to work without looping through the list but that wouldn't be ideal in an if statement.
以为我会来这里寻求帮助。
【问题讨论】:
-
你的表名是“-1”、“-2”、“-3”、....?
-
@jdweng 是的,他们是。抱歉,我应该说得更清楚。
-
c#中的DataSet类有一个Contains方法来检查表名是否存在: if(imports.Tables.Contains("-1")) { enter code here }
-
@jdweng 这基本上就是我在上面使用的并且确实有效,只是希望有一种更清洁的方法,而不是需要一遍又一遍地重复声明。
标签: c# linq datatable contains