Linq to SQL匿名类型返回null

时间:2017-06-05 07:12:08

标签: c# linq linq-to-sql

我有一个返回多个结果集的查询。但如果其中一个结果为null,则不返回任何内容。即使此结果集为空,是否可以恢复数据?

示例:

from u in Units
select new
{
    Unit = u,
    AgreementType = from at in AgreementTypes
                    select new { at },
    OptionRightsType = from ortp in OptionRightsTypes
                       select new { ortp }
}

在此示例中,如果Unit为null,则不返回任何记录。即使unit为null,是否可以恢复其他结果集?

更新

示例:

from u in Units
where u.Id == 1000
select new
{
    Unit = u,
    AgreementType = from at in AgreementTypes
                    select new { at },
    OptionRightsType = from ortp in OptionRightsTypes
                       select new { ortp }
}

在上面的例子中,我将存在恢复三个IQueryable类型的结果,Unit,AgreementType,OptionRightsType。

就是这种情况,但如果查询找不到id等于1000的单位,则返回null。

当设备没有返回记录时,我还可以恢复AgreementType和OptionRightsType类型吗?

更新2

我想做的是:

from u in Units
where u.Id == 1000
select new
{
    Unit = u
}

from at in AgreementTypes
           select new 
           { 
               at,
               OptionRightsType = from ortp in OptionRightsTypes
                                  select new { ortp }
            }

两个完全独立的查询。

我只是认为我可以将它们连接在一起,因为第二个查询只是拉回静态数据

4 个答案:

答案 0 :(得分:1)

你需要的是:

var unit = Units.Where(u => u.Id == 1000).FirstOrDefault();
var agreementTypes = AgreementTypes.ToArray();
var optionRightsTypes = OptionRightsTypes.ToArray();

在您知道调用速度确实存在问题之前,请不要担心网络电话。

答案 1 :(得分:0)

如果你有一些关系,那么你可以使用左外连接:

var result = from u in units
                         join at in AgreementTypes on u equals at into A
                         from at in A.DefaultIfEmpty()
                         join ortp in OptionRightsTypes on u equals ortp into B
                         from ortp in B.DefaultIfEmpty()
                         where u.Id == 1000
                         select new
                         {
                             Unit = u ? u.SomeValue: "No units",
                             att = att ? att.SomeValue: "No att",
                             ortp = ortp.SomeValue ?? "No ortp"
                         };

答案 2 :(得分:0)

就我个人而言,我会说不要做你想做的事情,而是去看“电影”的回答

维护此代码的人不清楚其意图是什么。 它也可能不会产生您想要的数据结构。对于发现的每个单元,它将选择所有的AgreementType和OptionRightsType数据。这可能是相同数据的大量重复。

from u in Units
select new
{
    Unit = u,
    AgreementType = from at in AgreementTypes
                    select new { at },
    OptionRightsType = from ortp in OptionRightsTypes
                       select new { ortp }
}

根据您的Update 2将代码拆分为两个单独的查询,可以更清楚地了解您要实现的目标。

您正在获得所需的单位。

from u in Units
where u.Id == 1000
select new
{
    Unit = u
}

您将获得所有AgreementTypes和OptionRightsTypes,但不会复制找到的每个单元的数据。

from at in AgreementTypes
           select new 
           { 
               at,
               OptionRightsType = from ortp in OptionRightsTypes
                                  select new { ortp }
            }

可能你可以先问另一个问题,为什么你想这样做。听起来您正在尝试优化数据库调用。这可能是另一种方式。例如缓存静态数据。

如果您仍想尝试将这些减少到一个查询,那么我建议您先尝试在SQL中编写代码,然后查看该代码是否可以转换为Linq。

答案 3 :(得分:0)

根据给出的答案和我自己的代码的进一步测试,我可以根据我的更新示例在单个查询中将查找数据拉出数据库。我现在用它来从我的数据库中提取完整的模型,然后使用静态类将其转换为DTO。

我尝试过AutoMapper,但需要使用静态类在对象之间进行转换而获得额外的粒度。