具有多个连接语法错误的复杂LINQ查询

时间:2013-09-19 17:05:42

标签: c# linq asp.net-mvc-4 join

我有一个连接两个列表并创建一个新对象列表的查询,我希望通过一个新的连接来增强它,到目前为止,我尝试的所有内容似乎都没有用。

列表是:

IList<Content> result
IList<Content> resultDefault

查询是:

IEnumerable<ContentItem> joined = from x in resultDefault
join y in result on new { x.ResourceKey, x.ResourceType, x.Application } equals new { y.ResourceKey, y.ResourceType, y.Application } into g
from o in g.DefaultIfEmpty(new Content()
{
  Id = 0,
  CultureCode = cultureCode,
  ResourceKey = x.ResourceKey,
  ResourceType = x.ResourceType,
  ResourceValue = String.Empty
})

where
 (
     resourcesJoin == ResourcesJoin.All ||
     (resourcesJoin == ResourcesJoin.Translated && o.ResourceValue != String.Empty) ||
     (resourcesJoin == ResourcesJoin.NotTranslated && o.ResourceValue == String.Empty)
  )
  &&
  (
     string.IsNullOrEmpty(searchString) ||
     (
         x.ResourceKey.ContainsIgnoreCase(searchString) ||
         x.ResourceValue.ContainsIgnoreCase(searchString) ||
         o.ResourceValue.ContainsIgnoreCase(searchString)
     )
  )
select new ContentItem(o, x);

到目前为止效果很好。

现在,我想添加第三个列表

IList<DifferentContent> resultCollection;

我已经扩展了ContentItem以获取DifferentContent的第三个参数

所以我想要这样的东西:

IEnumerable<ContentItem> joined = from x in resultDefault
    join y in result on new { x.ResourceKey, x.ResourceType, x.Application } equals new { y.ResourceKey, y.ResourceType, y.Application } into g
    from o in g.DefaultIfEmpty(new Content()
    {
      Id = 0,
      CultureCode = cultureCode,
      ResourceKey = x.ResourceKey,
      ResourceType = x.ResourceType,
      ResourceValue = String.Empty
    })
    join z in resultCollection on new { x.ResourceKey, x.ResourceType, x.Application, x.CultureCode } equals new { z.ResourceKey, z.ResourceType, z.Application, defaultCultureCode } into m
    from s in m.DefaultIfEmpty(new DifferentContent()
      {
          Id = 0,
          CultureCode = defaultCultureCode,
          ResourceKey = x.ResourceKey,
          ResourceType = x.ResourceType,
          ResourceValue = string.Empty
      })
    where
     (
         resourcesJoin == ResourcesJoin.All ||
         (resourcesJoin == ResourcesJoin.Translated && o.ResourceValue != String.Empty) ||
         (resourcesJoin == ResourcesJoin.NotTranslated && o.ResourceValue == String.Empty)
      )
      &&
      (
         string.IsNullOrEmpty(searchString) ||
         (
             x.ResourceKey.ContainsIgnoreCase(searchString) ||
             x.ResourceValue.ContainsIgnoreCase(searchString) ||
             o.ResourceValue.ContainsIgnoreCase(searchString)
         )
      )
    select new ContentItem(o, x, s);

但这是无效的。错误是:

 The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'GroupJoin'.

Resharper给出了“方法的类型参数......不能被推测”。不幸的是,我无法复制粘贴。

正确的连接语法是什么?

0 个答案:

没有答案