1
    private string FindTaxItemLocation(string taxItemDescription)
    {
        if (!templateDS.Tables.Contains(cityStateTaxesTable.TableName))
            throw new Exception("The schema dos not include city state employee/employer taxes table");
        var cityStateTaxes =
            templateDS.Tables[cityStateTaxesTable.TableName].AsEnumerable().FirstOrDefault(
                x => x.Field<string>(Fields.Description.Name) == taxItemDescription);//[x.Field<string>(Fields.SteStateCodeKey.Name)]);

        if (cityStateTaxes != null)
            return cityStateTaxes[Fields.SteStateCodeKey.Name].ToString();

        return null;
    }

cityStateTaxes は DataRow ですが、FirstOrDefault() 内で列の値を取得できないのはなぜですか?

ありがとう、

4

1 に答える 1

1

FirstOrDefault()コレクション内の最初の項目を選択する (オプションで述語を満たす) かnull、そこが空である (または述語を満たすものが何もない) 場合は戻ります。それはあなたのために予測を行いません。したがって、それを使用すると、デフォルト値のチェックを含める必要があるため、項目のフィールドにアクセスするのが面倒になる可能性があります。

私の提案は、 を使用する前に常に目的のフィールドFirstOrDefault()に射影することです。そうすれば、チェックを実行する必要なくフィールドをまっすぐにすることができます。

var cityStateTaxes = templateDS.Tables[cityStateTaxesTable.TableName]
    .AsEnumerable()
    .Where(row => row.Field<string>(Fields.Description.Name) == taxItemDescription) // filter the rows
    .Select(row => row.Field<string>(Fields.SteStateCodeKey.Name)) // project to your field
    .FirstOrDefault(); // you now have your property (or the default value)

return cityStateTaxes;
于 2011-06-24T22:52:26.230 に答える