实体框架增加了组的索引属性

时间:2013-12-22 15:22:42

标签: c# linq entity-framework

我想为一个独立的每个项目增加一个int属性(如here所述),因为引号需要像/person/quote/1..2..3而不是/person/quote/1..5..10一样可访问:< / p>

Quote  Person  Index
Lorem   Smith  1
Ipsum   Smith  2
Loremi  Lewis  1
Ipsumi  Lewis  2

在EF中使用该问题中的代码:

var query = _data.Quotes
    .GroupBy(x => x.Person.Name)
    .Select
    (
        x => x.Select((y, i) => new { y.Text, y.Person.Name, Index = i + 1 })
    )
    .SelectMany(x => x);

但EF无法解析它并返回NotSupportedException异常:

LINQ to Entities does not recognize the method
System.Collections.Generic.IEnumerable`1[<>f__AnonymousType9`2[System.String,System.Int32]] Select[Quote,<>f__AnonymousType9`2](System.Collections.Generic.IEnumerable`1[App.Models.Quote], System.Func`3[App.Models.Quote,System.Int32,<>f__AnonymousType9`2[System.String,System.Int32]]) and this method cannot be translated into a store expression

2 个答案:

答案 0 :(得分:2)

感谢 Dabblernl的评论,此代码正常运作:

var query = _data.Quotes
    .GroupBy(x => x.Person.Name)
    .ToList()
    .Select
    (
        x => x.Select((y, i) => new { y.Person.Name, y.Text, Index = i + 1 })
    )
    .SelectMany(x => x);

答案 1 :(得分:0)

查询:

 var query = _data.Quotes
  .GroupBy(x => x.Person.Name.ToLower())
  .Select
  (
    x => x.Select((y, i) => new { y.Text.ToLower(), y.Person.Name.ToLower(), Index = i + 1 })
  )
  .SelectMany(x => x);