2

嘿,我有以下查询,即

    public int GetDefaultCurrencyForOrganization(string organizationId)
    {
        return (from a in _dataContext.allocations
                where a.organizationId.Equals(organizationId)
                select a.currencyId).SingleOrDefault();
    }

对于很多公司来说,这可能没有任何回报,这是我想将值设置为 4 来返回。只是想知道如何将其添加到上面?

4

2 回答 2

2

我想你正在寻找这样的东西:

public int GetDefaultCurrencyForOrganization(string organizationId)
{
    return (from a in _dataContext.allocations
            where a.organizationId.Equals(organizationId)
            select (int?)a.currencyId).SingleOrDefault() ?? 4;
}

你也可以这样做:

public int GetDefaultCurrencyForOrganization(string organizationId)
{
    var alloc = _dataContext.allocations
        .SingleOrDefault(a => a.organizationId.Equals(organizationId));
    return alloc != null ? alloc.currencyId : 4;
}
于 2013-06-20T19:02:02.340 回答
2

尝试使用 DefaultIfEmpty(4) 而不是 SingleOrDefault 来指定一个值。:

public int GetDefaultCurrencyForOrganization(string organizationId)
{
    return (from a in _dataContext.allocations
            where a.organizationId.Equals(organizationId)
            select a.currencyId).DefaultIfEmpty(4).Single();
}

http://msdn.microsoft.com/en-us/library/bb355419.aspx

于 2013-06-20T19:02:25.287 回答