我加入了2的内存收集
var items =
from el in elements
join def in Cached.Elements()
on el.Value equals def.Name into temp1
from res in temp1.DefaultIfEmpty()
select new
{
el.NodeType,
res.DefKey,
res.DefType,
res.BaseKey,
el.Value
};但是,理想情况下,如果找不到其中一个元素,我希望引发一个异常,类似于
throw new System.Exception(el.Value + " cannot be found in cache!");我在看System.Interactive,它提供了一个Catch扩展方法,但我不确定如何在该上下文中引用当前的'el‘。例如,我想知道像这样的事情
var items = (
from el in elements
join def in Cached.Elements()
on el.Value equals def.Name into temp1
from res in temp1.DefaultIfEmpty()
select new
{
el.NodeType,
res.DefKey,
res.DefType,
res.BaseKey,
el.Value
})
.ThrowIfEmpty();但是,istm,这将需要将整个集合传递到扩展方法中,而不是在遇到缺失值时引发异常。
或者,我也可以用ThrowIfEmpty替换DefaultIfEmpty
var items = (
from el in elements
join def in Cached.Elements()
on el.Value equals def.Name into temp1
from res in temp1.ThrowIfEmpty()
select new
{
el.NodeType,
res.DefKey,
res.DefType,
res.BaseKey,
el.Value
});有没有一种“合适的”/更好的方法来做到这一点?
发布于 2012-02-24 15:45:21
您可以使用GroupJoin。这样的东西对你来说应该是有效的:
elements.GroupJoin(Cached.Elements(), e => e.Value, d => d.Name, (e, dSeq) => {
var d = dSeq.Single();
return new { e, d };
});GroupJoin resultSelector接受两个参数:左键和匹配的右键序列。如果序列为空,则可以引发异常;实现此目的的一种方法是使用单个运算符。
发布于 2012-02-24 16:20:02
我认为这是你可以使用Composite Keys的地方之一。
如果使用equals关键字在联接上执行相等。
来自文档:
您可以将组合键创建为匿名类型或具有要比较的值的命名类型。如果要跨方法边界传递查询变量,请使用重写键
的Equals和GetHashCode的命名类型
https://stackoverflow.com/questions/9427053
复制相似问题