“序列中没有匹配的元素”,我在执行Linq代码时遇到此错误

时间:2019-04-23 12:03:08

标签: c# asp.net linq

在尝试使用LINQ查询进行比较和分配某些数据时,出现“序列不包含匹配元素”的信息

cfgData.Rows.Cast<DataRow>().ToList().
  ForEach(row => row["Name"] =
            valData.Rows.Cast<DataRow>().
              First(x => Convert.ToInt32(row["AccountTypeId"]).
                           Equals(Convert.ToInt32(x["Id"])))["AccountType"]);

在上下文中:

public static object DeployAccountTypes()
{
  using (SqlConnection cfgConn = Connection.GetConnection(NamedConnection.Configuration))
  using (SqlConnection valConn = Connection.GetConnection(NamedConnection.Validation))
  using (SqlCommand cmd = new SqlCommand("SELECT *, '' as Name FROM tcRAPIDAccountTypes (NOLOCK) ORDER BY EvaluationOrder", cfgConn))
  using (SqlCommand cmd1 = new SqlCommand("SELECT * FROM tcRAPIDLoadAccountTypes (NOLOCK)", valConn))
  {
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable cfgData = new DataTable();
    da.Fill(cfgData);

    da = new SqlDataAdapter(cmd1);
    DataTable valData = new DataTable();
    da.Fill(valData);

    // ERROR HERE
    cfgData.Rows.Cast<DataRow>().ToList().
      ForEach(row => row["Name"] =
                valData.Rows.Cast<DataRow>().
                  First(x => Convert.ToInt32(row["AccountTypeId"]).
                               Equals(Convert.ToInt32(x["Id"])))["AccountType"]);

来自SELECT * FROM tcRAPIDLoadAccountTypes (NOLOCK)的数据:

Id  AccountType AccountTypeDescription  LOB
1   ABDNPROP    NULL                    AP
2   ASR         NULL                    CAS
4   BBUS        NULL                    CAS
5   BRETL       NULL                    CAS
6   BUSFLPOA    NULL                    CAS
7   BVICMN      NULL                    CAS
8   CORP        NULL                    CAS
9   CPSACCT     NULL                    CPS
10  DEFAULT     NULL                    CODG

来自SELECT *, '' as Name FROM MLSNAPS_XTCMConfig.dbo.tcRAPIDAccountTypes (NOLOCK) ORDER BY EvaluationOrder的数据:

Id  AccountTypeId   Description IsActive
6   45              NULL        1
7   46              NULL        1
8   16              NULL        1
9   23              NULL        1
10  22              NULL        1
11  49              NULL        1
12  20              NULL        1
13  18              NULL        1
14  17              NULL        1
15  15              NULL        1

1 个答案:

答案 0 :(得分:0)

尝试加入:

public static object DeployAccountTypes()
{
  using (SqlConnection cfgConn = Connection.GetConnection(NamedConnection.Configuration))
  using (SqlConnection valConn = Connection.GetConnection(NamedConnection.Validation))
  using (SqlCommand cmd = new SqlCommand("SELECT *, '' as Name FROM tcRAPIDAccountTypes (NOLOCK) ORDER BY EvaluationOrder", cfgConn))
  using (SqlCommand cmd1 = new SqlCommand("SELECT * FROM tcRAPIDLoadAccountTypes (NOLOCK)", valConn))
  {
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable cfgData = new DataTable();
    da.Fill(cfgData);

    da = new SqlDataAdapter(cmd1);
    DataTable valData = new DataTable();
    da.Fill(valData);

    var results = (from cfg in cfgData.AsEnumerable()
        join val in valData.AsEnumerable() on cfg.Field<int>("AccountTypeId") equals val.Field<int>("Id")
        select new { cfg = cfg, val = val })
        .ToList();
  }

}