SQL Server Comparing two list< T> objects and Updating the first object with second object using linq in c#

rqdpfwrv  于 2023-06-21  发布在  C#
关注(0)|答案(1)|浏览(140)

I have an Excel file with the set of records and I have a database called UserTable with a set of records. what I'm doing is comparing both the records with the field called UserId. there are some fields with null values and some with values in UserTable. If I compare the Excel with the database(UserTable) record it has to fetch the updated one with values in all fields. I have changed

I tried this:

public class UpdateData
    {
        public void Update(List<UserTable> users, List<UserTable> mappeddata)
        {
            //var result = from values in mappeddata join values2 in users on values.UserId equals values2.UserId select values;
            var valuestobeupdated = from m1 in mappeddata
                          join m2 in users on m1.UserId equals m2.UserId
                          where m1.ManagerUserId != m2.ManagerUserId
                            || m1.Department != m2.Department
                            || m1.Title != m2.Title ||  m1.Email != m2.Email || m1.FirstName != m2.FirstName    
                          select m1;
            Console.WriteLine("Updated");

        }
    }

but this is case sensitive for example if UserId=A24070 in users && UserId=a24070 in mappeddata it doesn't match, same goes with other field. Is there a way to make it case insensitive for example I did this in another statement var userIdnotinTable=userIdinExcel.Except(userIdinTable, StringComparer.OrdinalIgnoreCase); is there any example available for this? or is there any other way to do this?

thanks!!!

xggvc2p6

xggvc2p61#

You could always .ToLower() both sides of the equation, but depending on the amount of data it could bring a noticable performance impact.

Edit:

When considering performance, it is important to understand if the task is executed periodically or just once (or a couple of times) manually:

Converting two string of ~8 characters to lowercase x 200,000 is still not a great feat for a modern system, but if this task is to be executed, let's say, every 10 minutes, then the additional ms would start to add up and be noticable in CPU time (which further down the line means more server costs).

If you need to do this once - absolutly go for it, I'd say.

相关问题