【发布时间】:2017-04-09 12:57:18
【问题描述】:
假设我有一个包含个人和公司数据的表,如果没有找到个人或公司的数据,我想检索默认值,例如,假设我有一个表
CustomerAccountId CustomerAccountType DueDays IsAdjustable isDefaultForIndividual isDefaultForCompany
1 Individual 10 true false false
2 Company 20 false false false
null null 5 false true false
null null 30 true false true
我想创建一个函数,它接受两个参数IndividualCustomerAccountId 和CompanyCustomerAccountId,如果在表中找到IndividualCustomerAccountId,则检索它,如果未找到,则检索个人的默认值,即third row,在这种情况下为@在这种情况下为 987654327@CompanyCustomerAccountIdis found retrieve it, if not get the default value for the companies which isfourth row`。
假设我们创建了一个函数,它接受IndividualCustomerAccountId 作为第一个参数,CompanyCustomerAccountId 作为第二个参数
样本输入
MyFunc(1 , 2) 应该返回 first and second rows
MyFunc(1 , 50) 应该返回first and fourth rows,因为在表中没有找到CustomerAccountId 50 的公司,因此请检索公司的默认值fourth row
MyFunc(100 , 2) 应该返回 second and third rows,因为没有找到客户帐户 ID 为 100 的个人,所以我们得到个人的默认值 third row,我们有一个 customerAccountId 2 的公司,所以我们可以简单地从表中检索它。
我想创建一个 LINQ 查询或 SQL 函数来实现这些结果
【问题讨论】:
-
我会假设规范化您的数据不是一种选择?更好的设计会更容易查询。
-
@Crowcoder 你是对的,但不幸的是我们现在不能改变数据库设计
标签: c# sql sql-server linq