C# Linq到SQL匿名类型返回null
我有一个返回多个结果集的查询。但如果其中一个结果为null,则不会返回任何结果。即使此结果集为空,也可以恢复数据吗 例如:C# Linq到SQL匿名类型返回null,c#,linq,linq-to-sql,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
from u in Units
select new
{
Unit = u,
AgreementType = from at in AgreementTypes
select new { at },
OptionRightsType = from ortp in OptionRightsTypes
select new { ortp }
}
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 }
}
在本例中,如果Unit为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 }
}
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
}
及
两个完全不同的查询
我只是想我可以将它们连接在一起,因为第二个查询只是将静态数据拉回来如果您有一些关系,那么您可以使用left-outer-join:
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"
};
听起来你需要的是:
var unit = Units.Where(u => u.Id == 1000).FirstOrDefault();
var agreementTypes = AgreementTypes.ToArray();
var optionRightsTypes = OptionRightsTypes.ToArray();
在您知道自己在通话速度方面存在实际问题之前,不要担心网络通话。我个人认为,不要做您想做的事情,而要接受Enigmativity的答案 对于维护此代码的人来说,不清楚其意图是什么。 此外,它可能不会产生您想要的数据结构。对于找到的每个单元,它将选择所有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 }
}
根据更新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 }
}
也许你可以问另一个问题,为什么你一开始就想这么做。听起来您正在尝试优化数据库调用。这可以通过另一种方式实现。e、 g.缓存静态数据
如果您仍然想尝试将这些查询简化为一个查询,那么我建议您首先尝试用SQL编写代码,然后查看该代码是否可以转换为Linq。根据给出的答案和对我自己代码的进一步测试,我可以按照更新的示例,在单个查询中从数据库中提取查找数据。我现在使用它从数据库中提取完整的模型,然后使用静态类将其转换为DTO
我尝试了AutoMapper,但需要通过使用静态类在对象之间转换获得的额外粒度。您到底想要实现什么?您的查询被破坏了。
选择新的{at}
或选择新的{ortp}
是没有意义的。您可以分别执行select at
和select ortp
。但是你可以只做AgreementType=AgreementTypes
&OptionRightsType=OptionRightsType
。但这很奇怪。我想您可能希望AgreementType
和optionrightsype
成为基于u
的连接。是吗?你到底想达到什么目的还不清楚。对于单元
的每个实例,您的查询只会返回所有协议类型
和选项权限类型
。这是没有道理的。你能更清楚地说明你希望通过你的查询得到什么吗?谜一样,我只是想减少对数据库的调用次数。因此,在我无知的情况下,我想,啊,在查询中添加额外的必需查找。所以AgreementType和OptionRightsType就是这样。它们将用作某些下拉列表的查找。请参阅msdn left outer join:没有关系,我希望通过收集所需的数据来减少流量,包括用于视图上下拉列表的数据以及一次linq to sql调用