DO 5.0.20

using System;
using System.Diagnostics;
using System.Linq;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;
using Xtensive.Sql;
using Xtensive.Sql.Dml;

internal class Program
{
    private static void Main(string[] args)
    {
        var dc = new DomainConfiguration("sqlserver", "Data Source=.; Initial Catalog=DO40-Tests; Integrated Security=True;");

        dc.Types.Register(typeof(Status));
        dc.Types.Register(typeof(Style));
        dc.Types.Register(typeof(Helper));

        dc.UpgradeMode = DomainUpgradeMode.Recreate;

        using (var d = Domain.Build(dc))
        {
            using (var s = d.OpenSession())
            using (s.Activate())
            using (s.OpenTransaction())
            {
                var ids = new[] { Guid.NewGuid() };

                Query.All<Status>().Any(it => Query.All<Style>().SingleOrDefault(e => e.Id.AsString() == it.Name).Id == ids[0]); //OK
                Query.All<Status>().Any(it => Query.All<Style>().SingleOrDefault(e => e.Id.AsString() == it.Name).Id.In(ids));   //FAIL
            }
        }
    }

    [Serializable]
    [KeyGenerator(KeyGeneratorKind.None)]
    public abstract class EntityBase : Entity
    {
        protected EntityBase(Guid id) : base(id)
        {
            if (id == Guid.Empty)
            {

            }
        }

        [Field] 
        [Key] 
        public Guid Id { get; private set; }
    }

    [HierarchyRoot]
    [Index("Name", Unique = true, Name = "Status.IX_Name")]
    [Serializable]
    [DebuggerDisplay("[Status] {Name}")]
    public class Status : EntityBase
    {
        public Status(Guid id)
            : base(id)
        {
        }

        [Field(Nullable = false)] 
        public string Name { get; set; }

        [Field] 
        public Style Style { get; set; }
    }

    [HierarchyRoot]
    [Serializable]
    public class Style : EntityBase
    {
        public Style(Guid id)
            : base(id)
        {
        }

        [Field] 
        public string Name { get; set; }
    }
}

[CompilerContainer(typeof(SqlExpression))]
public static class Helper
{
    public static string AsString(this Guid id)
    {
        return id.ToString();
    }

    [Compiler(typeof(Helper), nameof(AsString), TargetKind.Method | TargetKind.Static)]
    public static SqlExpression CompileAsString(SqlExpression id)
    {
        return SqlDml.Cast(id, SqlType.VarChar, 36);
    }
}

Exception:

Unhandled Exception: Xtensive.Orm.StorageException: SQL error occured.
SQL error details 'Type: Unknown;'
Query 'SELECT CAST((CASE WHEN EXISTS (SELECT * FROM (SELECT [a].[Id], 100 AS [TypeId], [a].[Name], [a].[Style.Id] FROM [dbo].[Program.Status] [a]) [b] WHERE ([b].[TypeId] IN (@p0_0_0_0))) THEN 1 ELSE 0 END)  AS bit) AS [c01umn1]; [p0_0_0_0='107456d5-cb92-
4be6-a13e-0a414727091b']'
Original message 'Failed to convert parameter value from a Guid to a Int32.' ---> System.InvalidCastException: Failed to convert parameter value from a Guid to a Int32. ---> System.InvalidCastException: Object must implement IConvertible.
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
   at System.Data.SqlClient.SqlParameter.GetCoercedValue()
   at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
   at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters, Boolean includeReturnValue)
   at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParamet
erEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolea
n inRetry)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Xtensive.Orm.Providers.StorageDriver.<ExecuteReader>b__11(DbCommand c)
   at Xtensive.Orm.Providers.StorageDriver.ExecuteCommand[TResult](Session session, DbCommand command, Func`2 action)
   --- End of inner exception stack trace ---
   at Xtensive.Orm.Providers.StorageDriver.ExecuteCommand[TResult](Session session, DbCommand command, Func`2 action)
   at Xtensive.Orm.Providers.StorageDriver.ExecuteReader(Session session, DbCommand command)
   at Xtensive.Orm.Providers.Command.ExecuteReader()
   at Xtensive.Orm.Providers.BatchingCommandProcessor.ExecuteBatch(Int32 numberOfTasks, QueryRequest lastRequest)
   at Xtensive.Orm.Providers.BatchingCommandProcessor.ExecuteTasksWithReader(QueryRequest request)
   at Xtensive.Orm.Providers.SqlSessionHandler.<Xtensive.Orm.Providers.IProviderExecutor.ExecuteTupleReader>d__2.MoveNext()
   at Xtensive.Orm.Providers.SqlProvider.<OnEnumerate>d__9.MoveNext()
   at Xtensive.Orm.Rse.Providers.ExecutableProvider.<GetEnumerator>d__0.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Xtensive.Orm.Rse.RecordSet.<GetGreedyEnumerator>d__0.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Xtensive.Core.EnumerableExtensions.<Batch>d__e`1.MoveNext()
   at Xtensive.Core.EnumerableExtensions.<ApplyBeforeAndAfter>d__16`1.MoveNext()
   at System.Linq.Enumerable.<SelectManyIterator>d__17`2.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at lambda_method(Closure , Object[] , IEnumerable`1 , Session , Dictionary`2 , ParameterContext )
   at Xtensive.Core.DelegateBindExtensions.<>c__DisplayClassd`6.<Bind>b__c(T2 arg2, T3 arg3, T4 arg4, T5 arg5)
   at Xtensive.Orm.Linq.TranslatedQuery`1.Execute(Session session, ParameterContext parameterContext)
   at Xtensive.Orm.Linq.QueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
   at Sample.Program.Main(String[] args) in C:\Projects\CleanDO\Sample\Program.cs:line 32

asked Feb 18 at 06:12

Gushchin%20Anton's gravatar image

Gushchin Anton
11142629

Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA