The following example leads to Xtensive.Orm.QueryTranslationException
public class Program
{
public static void Main(string[] args)
{
var dc = new DomainConfiguration("sqlserver", "Data Source=.; Initial Catalog=DO40-Tests; Integrated Security=True;");
dc.Types.Register(typeof(TestEntity));
dc.Types.Register(typeof(TestEntity2));
dc.UpgradeMode = DomainUpgradeMode.Recreate;
using (var d = Domain.Build(dc))
{
using (var s = d.OpenSession())
using (s.Activate())
using (s.OpenTransaction())
{
var test = new TestEntity2(s) { Str = "test" };
new TestEntity(s) { TestEntity2 = test, Str = "test" };
// This query is successfull
var items1 = Query.All<TestEntity>()
.Select(it => new
{
Test = it.TestEntity2 != null
? string.Empty
: it.TestEntity2.Str
})
.Select(it => it.Test == null || it.Test.Length < 10
? it.Test
: it.Test.Substring(0, 10))
.ToList();
// This query gets exception
var items2 = Query.All<TestEntity>()
.Select(it => new
{
Test = it.TestEntity2 != null
? (string)null
: it.TestEntity2.Str
})
.Select(it => it.Test == null || it.Test.Length < 10
? it.Test
: it.Test.Substring(0, 10))
.ToList();
}
}
}
[HierarchyRoot]
public class TestEntity : Entity
{
/// <summary>Initializes a new instance of this class.</summary>
/// <param name="session">The session.</param>
public TestEntity(Session session) : base(session)
{
}
[Key]
[Field(Nullable = false)]
public int Id { get; private set; }
/// <summary>
/// TestEntity2
/// </summary>
[Field]
public TestEntity2 TestEntity2 { get; set; }
/// <summary>
/// Str
/// </summary>
[Field]
public string Str { get; set; }
}
[HierarchyRoot]
public class TestEntity2 : Entity
{
/// <summary>Initializes a new instance of this class.</summary>
/// <param name="session">The session.</param>
public TestEntity2(Session session) : base(session)
{
}
[Key]
[Field(Nullable = false)]
public int Id { get; private set; }
/// <summary>
/// Str
/// </summary>
[Field]
public string Str { get; set; }
}
}
asked
Oct 20 '21 at 03:23
Vasily Smirnov
5●1●1●2