不正确地创建了应用加入

时间:2017-04-11 18:37:42

标签: linq sqlite entity-framework-6 linq-to-entities system.data.sqlite

给出以下sqlite表

CREATE TABLE `ComponentNameLookup` (
    `Id`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `ComponentId`   INTEGER NOT NULL,
    `ComponentName` TEXT NOT NULL,
    `Culture`   TEXT
);

insert into ComponentNameLookup
    (Id,ComponentId,ComponentName,Culture)
values
    (1,  0, 'Logger',                  NULL  ),
    (2,  1, 'Transport',               NULL  ),
    (3,  2, 'Error Handler',           NULL  ),
    (4,  3, 'Persistance',             NULL  ),
    (5,  0, 'Registrador',            'es-ES'),
    (6,  1, 'Transporte',             'es'   ),
    (7,  2, 'Controlador de errores', 'es-ES'),
    (8,  3, 'Persistencia',           'es-ES'),
    (9,  1, 'Транспорт',              'ru'   ),
    (10, 2, 'Обработчик ошибок',      'ru-RU')

和Linq查询

void Main()
{
    string cultureString = Thread.CurrentThread.CurrentCulture.Name;
    string languageName = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

    cultureString = "es-ES";
    languageName = "es";

    var localized = context.ComponentNameLookups
        .Where(x => x.Culture == cultureString || x.Culture == languageName || x.Culture == null)
        .GroupBy(c => c.ComponentId)
        .Select(g => new KeyValue<int?, string>{
            Key = g.Key,
            Value = g
                .OrderByDescending(x => x.Culture.Length)
                .Select(c => c.ComponentName)
                .FirstOrDefault(),
        })
    ;
    localized.ToArray();
}

public class KeyValue<T1, T2> {
    public T1 Key;
    public T2 Value;
}

我收到错误

  

System.Data.Entity.Core.EntityCommandCompilationException:准备命令定义时发生错误。有关详细信息,请参阅内部异常---&GT; System.NotSupportedException:不支持APPLY联接

这样的东西不应该是一个APPLY JOIN。有没有其他方法可以使用LINQ进行此查询?

1 个答案:

答案 0 :(得分:1)

我无法在SqlLite上进行测试,但查询有(虽然不同)MySQL问题(它可以在SqlServer上正常运行),所以你可以尝试替换

.OrderByDescending(x => x.Culture.Length)

.Where(c => c.Culture.Length == g.Max(e => e.Culture.Length))

修复了MySQL问题并从SqlServer查询中删除了OUTER APPLY,因此它也适用于SqlLite。