1

トランザクションをフィルタリングするこの Linq ステートメントがあります。フィルタリングすると正常に動作しますが、何も返されない場合は dt.AsEnumerable() でエラーが発生します。

エラーは、データには行が含まれていません。何も返されない場合の処理​​方法を知っている人はいますか?

      newDataTable = dt.AsEnumerable()
                    .Where(r => !ListLinkedIds.Contains(r.Field<int>("LinkedTicketId")))
                    .CopyToDataTable();  

      gvMain.DataSource = newDataTable;
      gvMain.DataBind();
4

2 に答える 2

3

You cannot use CopyToDataTable if the input sequence is empty. So you need to check that first:

var newDataTable = dt.Clone();  // an empty table with the same schema
var ticketRows = dt.AsEnumerable()
    .Where(r => !ListLinkedIds.Contains(r.Field<int>("LinkedTicketId")));
if(ticketRows.Any())
    newDataTable = ticketRows.CopyToDataTable();

Possible exceptions with CopytoDataTable

  • ArgumentNullException
    The source IEnumerable sequence is null and a new table cannot be created.
  • InvalidOperationException
    • A DataRow in the source sequence has a state of Deleted.
    • The source sequence does not contain any DataRow objects.
    • A DataRow in the source sequence is null.
于 2013-08-02T20:28:58.167 に答える
0

DataTable呼び出す前に行があるかどうかを確認してくださいAsEnumerable()

    if (dt.Rows.Count > 0)
    {
      newDataTable = dt.AsEnumerable()
                    .Where(r => !ListLinkedIds.Contains(r.Field<int>("LinkedTicketId")))
                    .CopyToDataTable();  

      gvMain.DataSource = newDataTable;
      gvMain.DataBind();
    }
    else {
    //error
    }
于 2013-08-02T20:16:57.620 に答える