Error running a select when converting to int?

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Paul_Ibis
Posts: 20
Joined: Wed 22 Jan 2020 03:02

Error running a select when converting to int?

Post by Paul_Ibis » Tue 10 Mar 2020 03:41

I have just updated our code base from LinqToSQL to Devart LinqConnect. Version 4.9.1920
Previously we had this code which ran OK in LinqToSQL. It converted to a list of int? as further down the code is a Contains on the list which takes a parameter that is of type int?

Code: Select all

IQueryable<Account> internalAccounts = CompanyHelper.InternalAccounts(sc);
List<int?> accs = internalAccounts.Select(p => (int?)p.PrimaryKey).ToList();
This now fails on the second line with the error : System.ArgumentException: 'Object of type 'Devart.Data.Linq.Engine.b9[System.Int32]' cannot be converted to type 'System.Collections.Generic.IEnumerator`1[System.Nullable`1[System.Int32]]'.'

I can change the code so it just creates a List<int> and then change the other code lower down in the Contains easily enough, but just wondering if this is a bug or just something that LinqToSQL supports but Devart does not.

thanks

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Error running a select when converting to int?

Post by Shalex » Tue 17 Mar 2020 11:50

Does your PrimaryKey property include IsPrimaryKey="true" in its mapping?

We cannot reproduce the error with the following code (DEPTNO has IsPrimaryKey="true"):

Code: Select all

    IQueryable<Dept> entities = context.Depts;
    List<int?> ids = entities.Select(p => (int?)p.DEPTNO).ToList();

Paul_Ibis
Posts: 20
Joined: Wed 22 Jan 2020 03:02

Re: Error running a select when converting to int?

Post by Paul_Ibis » Tue 17 Mar 2020 21:47

I think I can see the possible issue. PrimaryKey in the underlying table is a computed column that was added to legacy tables in the database to conform to a new naming convention that was applied some time later.
So the create table in SQL would be something like this

Code: Select all

CREATE TABLE [dbo].[AccountTest]
(
	[AccountID] [int] IDENTITY(1000,1) NOT NULL,
	[PrimaryKey]  AS ([AccountID]),
	CONSTRAINT [PK_AccountTest] PRIMARY KEY CLUSTERED 
	(
		[AccountID] ASC
	)
)
if I change this

Code: Select all

 List<int?> accs = internalAccounts.Select(p => (int?)p.PrimaryKey).ToList();
to select the AccountID instead, I still get the same error message.
The declaration for the AccountID column is as below

Code: Select all

[[Column(Storage = "_AccountID", AutoSync = AutoSync.OnInsert, CanBeNull = false, DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
The declaration for the Primarykey column is as below

Code: Select all

 [Column(Storage = "_PrimaryKey", AutoSync = AutoSync.Always, CanBeNull = false, DbType = "INT NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Error running a select when converting to int?

Post by Shalex » Tue 31 Mar 2020 16:08

It works in our environment. Please send us a small test project for reproducing the issue via our contact form.

Paul_Ibis
Posts: 20
Joined: Wed 22 Jan 2020 03:02

Re: Error running a select when converting to int?

Post by Paul_Ibis » Fri 03 Apr 2020 04:47

Create the following table in SQL

Code: Select all

CREATE TABLE [dbo].[Test](
	[TestID] [int] IDENTITY(1000,1) NOT NULL,
	[PrimaryKey]  AS ([TestID]),
	[Code] [varchar](50) NULL,
 CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED 
(
	[TestID] ASC
)ON [PRIMARY])
GO
INSERT INTO Test(code)
SELECT 'test'
Then run this code :

Code: Select all

IQueryable<Test> tester = sc.DB.Test.Where(p => p.Code == "test");
 List<int?> accs = tester.Select(p => (int?)p.PrimaryKey).ToList();
You get the error on the second line. Note that if this is changed to

Code: Select all

 List<int?> accs = tester.Select(p => (int?)p.TestID).ToList();
you still get the same error.

This is the definition

Code: Select all

[Table(Name = @"dbo.Test")]
    public partial class Test : INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(System.String.Empty);
        #pragma warning disable 0649

        private int _TestID;

        private int _PrimaryKey;

        private string _Code;
        #pragma warning restore 0649

        #region Extensibility Method Definitions

        partial void OnLoaded();
        partial void OnValidate(ChangeAction action);
        partial void OnCreated();
        partial void OnTestIDChanging(int value);
        partial void OnTestIDChanged();
        partial void OnPrimaryKeyChanging(int value);
        partial void OnPrimaryKeyChanged();
        partial void OnCodeChanging(string value);
        partial void OnCodeChanged();
        #endregion

        public Test()
        {
            OnCreated();
        }

    
        /// <summary>
        /// There are no comments for TestID in the schema.
        /// </summary>
        [Column(Storage = "_TestID", AutoSync = AutoSync.OnInsert, CanBeNull = false, DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int TestID
        {
            get
            {
                return this._TestID;
            }
            set
            {
                if (this._TestID != value)
                {
                    this.OnTestIDChanging(value);
                    this.SendPropertyChanging("TestID");
                    this._TestID = value;
                    this.SendPropertyChanged("TestID");
                    this.OnTestIDChanged();
                }
            }
        }

    
        /// <summary>
        /// There are no comments for PrimaryKey in the schema.
        /// </summary>
        [Column(Storage = "_PrimaryKey", AutoSync = AutoSync.Always, CanBeNull = false, DbType = "INT NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
        public int PrimaryKey
        {
            get
            {
                return this._PrimaryKey;
            }
            set
            {
                if (this._PrimaryKey != value)
                {
                    this.OnPrimaryKeyChanging(value);
                    this.SendPropertyChanging("PrimaryKey");
                    this._PrimaryKey = value;
                    this.SendPropertyChanged("PrimaryKey");
                    this.OnPrimaryKeyChanged();
                }
            }
        }

    
        /// <summary>
        /// There are no comments for Code in the schema.
        /// </summary>
        [Column(Storage = "_Code", DbType = "VARCHAR(50)", UpdateCheck = UpdateCheck.Never)]
        public string Code
        {
            get
            {
                return this._Code;
            }
            set
            {
                if (this._Code != value)
                {
                    this.OnCodeChanging(value);
                    this.SendPropertyChanging("Code");
                    this._Code = value;
                    this.SendPropertyChanged("Code");
                    this.OnCodeChanged();
                }
            }
        }
   
        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            var handler = this.PropertyChanging;
            if (handler != null)
                handler(this, emptyChangingEventArgs);
        }

        protected virtual void SendPropertyChanging(System.String propertyName) 
        {
            var handler = this.PropertyChanging;
            if (handler != null)
                handler(this, new PropertyChangingEventArgs(propertyName));
        }

        protected virtual void SendPropertyChanged(System.String propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }


Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Error running a select when converting to int?

Post by Shalex » Wed 08 Apr 2020 20:44

Thank you for the additional information. We have reproduced the issue and are investigating it. We will notify you about the result.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Error running a select when converting to int?

Post by Shalex » Sat 20 Jun 2020 19:40

The bug with materializing elements, the types of which are explicitly converted from non-nullable to nullable on the client side, is fixed in LinqConnect v4.9.2003: viewtopic.php?f=31&t=41288.

Post Reply