不正确地创建了一个 Apply Join
Improperly created an Apply Join
给定以下 sqlite table
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: An error occurred while preparing the command definition. See the inner exception for details. ---> System.NotSupportedException: APPLY joins are not supported
像这样的东西不需要是一个 APPLY JOIN。有没有其他方法可以使用 LINQ 执行此查询?
我无法在 SqlLite 上进行测试,但查询与 MySQL 有(尽管不同)问题(它在 SqlServer 上运行良好),因此您可以尝试替换
.OrderByDescending(x => x.Culture.Length)
与
.Where(c => c.Culture.Length == g.Max(e => e.Culture.Length))
修复了 MySQL 问题并从 SqlServer 查询中消除了 OUTER APPLY
,因此它也适用于 SqlLite。
给定以下 sqlite table
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: An error occurred while preparing the command definition. See the inner exception for details. ---> System.NotSupportedException: APPLY joins are not supported
像这样的东西不需要是一个 APPLY JOIN。有没有其他方法可以使用 LINQ 执行此查询?
我无法在 SqlLite 上进行测试,但查询与 MySQL 有(尽管不同)问题(它在 SqlServer 上运行良好),因此您可以尝试替换
.OrderByDescending(x => x.Culture.Length)
与
.Where(c => c.Culture.Length == g.Max(e => e.Culture.Length))
修复了 MySQL 问题并从 SqlServer 查询中消除了 OUTER APPLY
,因此它也适用于 SqlLite。