合并在Linq

时间:2015-06-25 04:19:15

标签: c# .net linq

如果列没有数据,我正在尝试获取cashassignment_id列,然后打印0作为结果。但是Cash 1正在失踪。

Cash_DataTable数据:

cash_id    cash_name
1          cash 1
2          cash 2
3          cash 3

CashAssignments_DataTable数据:

cashassignment_id    cash_id   cashassignment_valid
100                  1         0 (false)
200                  1         0 (false)
300                  1         0 (false)

我目前的linq结果:

cash_id     cashassignment_id   cash_name
2           0                   cash 2
3           0                   cash 3

注意:Cash 1缺失,我需要在cashassignment_valid = true时显示所有现金,否则返回0为cashassignment_id

DataTable CashStatusProc1 = new DataTable();
CashStatusProc1.Columns.Add("cash_id", typeof(Int32));
CashStatusProc1.Columns.Add("cashassignment_id", typeof(Int32));
CashStatusProc1.Columns.Add("cash_name", typeof(String));

(from Cash in Cash_DataTable.AsEnumerable()
 join CashAssignments in CashAssignments_DataTable.AsEnumerable()
      on Cash.Field<Int32>("cash_id") equals CashAssignments.Field<Int32>("cash_id")
      into JoinedCashAssignments
 from CashAssignments in JoinedCashAssignments.DefaultIfEmpty()
      where ((((CashAssignments != null ? CashAssignments.Field<Boolean>("cashassignment_valid") : false) == true) || (CashAssignments == null)) )
     select new
               {
                  cash_id = Cash.Field<Int32>("cash_id"),
                  cashassignment_id = (CashAssignments != null ? CashAssignments.Field<Int32>("cashassignment_id") : 0),
                   cash_name = Cash.Field<String>("cash_name")
               }).Aggregate(CashStatusProc1, (dt, result) =>
                             {
                                 dt.Rows.Add(result.cash_id, result.cashassignment_id, result.cash_name); return dt;
                             });

1 个答案:

答案 0 :(得分:0)

这可能不是一个真正的答案,但它可能会引导您朝着正确的方向前进并处理一两个错误,让我们仔细检查您的代码。

在where声明中有几个难题。

((((CashAssignments != null ? CashAssignments.Field<Boolean>("cashassignment_valid") : false) == true) || (CashAssignments == null)) )

首先让它清理干净。 (翻了!=到==)

(CashAssignments == null ? false : 
CashAssignments.Field<Boolean>("cashassignment_valid")) == true
|| CashAssignments == null

现在让我们看一下,你要问CashAssignments是否等于null,如果是,那么你返回false,你检查CashAssignments等于false然后返回true 。这种意思与

相同
if (x == null || x != null) //Will always happen

(CashAssignments == null ? false : CashAssignments.Field<Boolean>("cashassignment_valid")) == true也有些多余,等于

bool z;
if(CashAssignments == null) z = false;
else z = CashAssignments.Field<Boolean>("cashassignment_valid")
if(z == true) // This is redundant just do the work in the else

所以从中删除|| CashAssignments == null并在第一个表达式

上处理它
(CashAssignments == null ? true : 
CashAssignments.Field<Boolean>("cashassignment_valid"))

实际上我们可以走得更远。你所拥有的可以被视为

x == null ? true : x.value //x.value might be true or false
// Equal to
if(x == null || x.value)

所以我们可以这样写(甚至更细,更可读)

CashAssignments == null || 
CashAssignments.Field<Boolean>("cashassignment_valid")

所以你实质上要求的是CashAssignments是否为空或CashAssignments.Field<Boolean>("cashassignment_valid")是否为真有效吗

接下来是选择表达式

cash_id = Cash.Field<Int32>("cash_id"),
cashassignment_id = (CashAssignments != null ? CashAssignments.Field<Int32>("cashassignment_id") : 0),
cash_name = Cash.Field<String>("cash_name")

这里只有一件感兴趣的事情cashassignment_id及其大部分内容都是为了便于阅读(翻页!= = = =主要是我自己的品味)

cashassignment_id = CashAssignments == null ? 0 : 
                        CashAssignments.Field<Int32>("cashassignment_id")

休息或多或少看起来不错。假设错误位于where语句中,并且可能已被清除和冗余删除,因为它们在多个区域中重叠,因此通过废弃您的代码。

其他注意事项:

在声明中,我声明为CashAssignments两次,一次是连接的结果,另一次是默认的结果,如果为空。

此外,由于您将其置于默认值,而不是null,您可以针对default(DataTable)进行测试,作为安全措施,以防止默认值不是null(对于很多情况,它为null,一个示例是int的默认值为0且不为空且0 == null equals to false