JG Vimalan's Blog

It is all about coding!

Compare two generic lists using Linq

I got a task where I need to compare 2 generic lists and find the modified records. Here is the source code,

We are going to compare ‘List<SalesOrder> oldSalesOrderList’ and ‘List<SalesOrder> sqlSalesOrderList’ and find out the modified records.

        public static List<Entities.SalesOrder> GetModifiedRecords(this List<SalesOrder> oldSalesOrderList, List<SalesOrder> sqlSalesOrderList)
        {
            var result = (from oraSalesOrder in oldSalesOrderList
                         from sqlSalesOrder in sqlSalesOrderList
                         where
                         ((oraSalesOrder.BillToAddress != sqlSalesOrder.BillToAddress ||
                         oraSalesOrder.BillToAddressId != sqlSalesOrder.BillToAddressId ||
                         oraSalesOrder.BillToCity != sqlSalesOrder.BillToCity ||
                         oraSalesOrder.BillToSite != sqlSalesOrder.BillToSite                         
                         )
                         && (oraSalesOrder.Id == sqlSalesOrder.Id &&
                         oraSalesOrder.LineNo == sqlSalesOrder.LineNo
                          ))
                         select oraSalesOrder).Distinct();

            List<SalesOrder> modifiedSalesOrder = new List<SalesOrder>(result);
            return modifiedSalesOrder;           
        }

Thats it! Now, you will get the modified records in a separate generic list.

Advertisements

April 16, 2011 Posted by | C#.NET, Linq | 4 Comments

Type of conditional expression cannot be determined because there is no implicit conversion between ‘null’ and ‘int’

I got the error ‘Type of conditional expression cannot be determined because there is no implicit conversion between ‘<null>’ and ‘int” in the following line of code,

        /// <summary>
        /// This method gets the null or integer value from the given object value
        /// </summary>
        /// <param name=”obj”></param>
        /// <returns></returns>
        public static Nullable<int> GetNullOrInt(object obj)
        {           
            return obj == null ? null : obj.ToInteger();
        }

In the above method, I am using a ternary operator to check if an object is null or not and return its integer value.

To fix this error, I have added another line of code as shown below,

        /// <summary>
        ///
        /// </summary>
        /// <param name=”obj”></param>
        /// <returns></returns>
        public static Nullable<int> GetNullOrInt(object obj)
        {
            Nullable<int> value = null;
            return obj == null ? value : obj.ToInteger();
        }

instead of returning the null value, I have created a nullable variable and used it in the return statement. This worked.

April 15, 2011 Posted by | C#.NET | 6 Comments

Re-distribute Oracle dll’s to Production/Client system via .NET setup application

I got a client requirement where a .NET setup has to be installed in several users system. This .NET setup installs a console application which connects to Oracle database and performs some action. In development environment, we have installed Oracle 11g client and it is must for connecting to Oracle database. But, it is not possible to request users, who are going to use the application to install Oracle client software in their system. So, I decided to re-distribute necessary Oracle dll’s to every user’s systems via the .NET setup project. By this way, user’s are no longer required to install Oracle client in their system to connect to Oracle database. Inorder to achieve that,

Add the following Oracle dll’s in the .NET setup project’s ‘Application Folder’.

oci.dll
oraociei11.dll
OraOps11w.dll
orannzsbb11.dll
Oracle.DataAccess.dll

I got the above dll’s from my development system where I have installed Oracle 11g client. The dll’s will be available under the “product\11.2.0\client_1” location in Oracle installation directory. After adding the dll’s, your setup project will look as shown below,

Thats all. Now, install the .NET setup in any user’s system and you will find that, the necessary oracle dll’s are copied to the installation directory and your application is able to connect to target Oracle database, even without the Oracle client installed.

April 1, 2011 Posted by | .NET Installer, Oracle | 6 Comments