开发者

Getting a column value

开发者 https://www.devze.com 2023-03-15 19:10 出处:网络
private string FindTaxItemLocation(string taxItemDescription) { if (!templateDS.Tables.Contains(cityStateTaxesTable.TableName))
    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 =
            te开发者_高级运维mplateDS.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 is a DataRow, why/how I cannot get the column value inside FirstOrDefault()?

Thanks,


FirstOrDefault() selects the first item in the collection (optionally that satisfies a predicate) or returns null in the case there it is empty (or nothing satisfies the predicate). It will not do projections for you. So if you use it, it can be awkward to access a field of the item since you must include default value checks.

My suggestion is to always project to your desired field(s) first before using FirstOrDefault(), that way you get your field straight without needing to perform the check.

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消