Search code examples
c#listinitializationienumerable

Reading from IEnumerable and adding items dynamically to List using foreach in c#


I'm using a stored proc(Dapper) to fetch data in an IEnumerable. To screen some unwanted characters and remove leading/trailing spaces, I have created a foreach loop, and then add the data to a List.

The following code is working and giving desired results, but I'm not sure if I'm doing it the right way or if there's a better way of doing it. Particularly, I'm sure if I'm initializing class appropriately and then adding to list.

public class LoadBOUserLogin
    {
        public string LOGIN_ID { get; set; }

        public string EMP_ID { get; set; }

        public string CODE { get; set; }
        public LoadBOUserLogin() { }
        public LoadBOUserLogin(string LOGIN_ID, string EMP_ID, string CODE)
        {
            this.CODE = CODE;
            this.LOGIN_ID = LOGIN_ID;
            this.EMP_ID = EMP_ID;
        }

    }  

private static IEnumerable<BOUserLogin> LoadBOUserLogin()
        {
            IEnumerable<DTO.BOUserLogin> items = new List<DTO.BOUserLogin>(0);

            var userLoginList = new List<BOUserLogin>();
            var userLogin = new BOUserLogin(); //initializing class
            try
            {

                string strQuery = @"[dbo].[BOUSER_SEL_Login]";


                using (SqlConnection con = new SqlConnection(Connections.GetConnectionString()))
                {
                    
                    items = con.Query<DTO.BOUserLogin>(strQuery, commandTimeout: 120, commandType: System.Data.CommandType.StoredProcedure);
                }

        //removing unwanted charaters and trim
                foreach (var x in items)
                {
                   
                    userLogin.LOGIN_ID = Regex.Replace(x.LOGIN_ID ?? string.Empty, @"[^\u0009\u000A\u000D\u0020-\u007E]", string.Empty).Trim();
                    userLogin.CODE = Regex.Replace(x.CODE ?? string.Empty, @"[^\u0009\u000A\u000D\u0020-\u007E]", string.Empty).Trim();
                    userLogin.EMP_ID = Regex.Replace(x.EMP_ID ?? string.Empty, @"[^\u0009\u000A\u000D\u0020-\u007E]", string.Empty).Trim();

                    userLoginList.Add(new BOUserLogin(userLogin.LOGIN_ID, userLogin.CODE, userLogin.EMP_ID));
                    
                }

            }
            catch (Exception ex)
            {
        throw ex;
            }
            
            return userLoginList;
        }

Solution

  • Close! First, I think your class should be named BOUserLogin, right?

    public class BOUserLogin
    {
        public string LOGIN_ID { get; }
        public string EMP_ID { get; }
        public string CODE { get; }
    
        public BOUserLogin() { }
    
        public BOUserLogin(string LOGIN_ID, string EMP_ID, string CODE)
        {
            this.CODE = CODE;
            this.LOGIN_ID = LOGIN_ID;
            this.EMP_ID = EMP_ID;
        }
    }
    

    Then your LoadBOUserLogin method needs a couple of minor changes, which I have commented in the code below:

    private static IEnumerable<BOUserLogin> LoadBOUserLogin()
    {
        IEnumerable<DTO.BOUserLogin> items = new List<DTO.BOUserLogin>(0);
    
        var userLoginList = new List<BOUserLogin>();
    
        // This is not required
        // var userLogin = new BOUserLogin(); //initializing class
    
        try
        {
            string strQuery = @"[dbo].[BOUSER_SEL_Login]";
    
            using (SqlConnection con = new SqlConnection(Connections.GetConnectionString()))
            {
                items = con.Query<DTO.BOUserLogin>(strQuery, commandTimeout: 120, commandType: System.Data.CommandType.StoredProcedure);
            }
    
            // Removing unwanted characters and trim
            foreach (var x in items)
            {
                // Assign the results to variables...
                var loginId = Regex.Replace(x.LOGIN_ID ?? string.Empty, @"[^\u0009\u000A\u000D\u0020-\u007E]", string.Empty).Trim();
                var code = Regex.Replace(x.CODE ?? string.Empty, @"[^\u0009\u000A\u000D\u0020-\u007E]", string.Empty).Trim();
                var empId = Regex.Replace(x.EMP_ID ?? string.Empty, @"[^\u0009\u000A\u000D\u0020-\u007E]", string.Empty).Trim();
    
                // ...and use the variables to instantiate the BOUserLogin class.
                userLoginList.Add(new BOUserLogin(loginId, code, empId));
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
        return userLoginList;
    }
    

    Finally, the catch has a couple of issues, which I will leave for you to investigate :)

    Hint 1: Search for 'rethrow to preserve stack trace'.

    Hint 2: Is the catch actually doing anything?