locked
problem to return boolean value RRS feed

  • Question

  • Hi All.

    I new in C#. I create method to return boolean value

    public bool Duplicate()
    {
       bool IsDuplicate = false;
       if (EmployeeFactory.GetDuplicate(search).Count() > 0)
          IsDuplicate = true;
    
       return IsDuplicate;
    }

    It is a very simple method and cannot figure out where is problem. When I run to debugging and use Watch window I see the variable IsDuplicate change value. But method did not indicate type and did not return value. Where is the problem and how to fix it?

    Thanks.


    • Edited by zleug Thursday, June 18, 2020 7:18 PM
    Thursday, June 18, 2020 7:17 PM

All replies

  • That particular code looks OK except it's not clear what the search is - is it a class property? May be there is an error in the GetDuplicate method and there is try/catch in that method which catches and not re-throws the error?

    Also, the method's name itself doesn't look good to me nor the call to the factory's method. If I would read that code, I would not understand it right away and the code needs to be self-documenting.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    • Edited by Naomi N Thursday, June 18, 2020 8:56 PM
    Thursday, June 18, 2020 8:55 PM
  • Here is a conceptual example using EF Core

    Looking for a count greater than 1

    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Example1.Contexts;
    using Example1.Models;
    using Microsoft.EntityFrameworkCore;
    
    namespace Example1.Classes
    {
        public class EntityOperations
        {
            /// <summary>
            /// Read rooms from database asynchronously
            /// </summary>
            /// <returns>List of rooms</returns>
            public static async Task<List<Room>> GetRooms()
            {
                var rooms = new List<Room>();
    
                await Task.Run(async () =>
                {
                    using (var context = new HotelContext())
                    {
                        rooms = await context.Room.AsNoTracking().ToListAsync();
                    }
                });
    
                return rooms;
            }
            public static async Task<bool> HasDuplications(int pKey)
            {
                int roomCount = 0;
    
                await Task.Run(async () =>
                {
                    using (var context = new HotelContext())
                    {
                        roomCount = await context.Room.AsNoTracking()
                            .Where(x => x.Identifier == pKey).CountAsync();
                    }
                });
    
                return roomCount >1;
    
            }
        }
    }
    

    usage

    var isDuplicate = await EntityOperations.HasDuplications(100);

    For greater than zero

    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Example1.Contexts;
    using Example1.Models;
    using Microsoft.EntityFrameworkCore;
    
    namespace Example1.Classes
    {
        public class EntityOperations
        {
            public static async Task<bool> HasDuplications(int pKey)
            {
                bool roomCount = false;
    
                await Task.Run(async () =>
                {
                    using (var context = new HotelContext())
                    {
                        roomCount = await context.Room
                            .AsNoTracking()
                            .AnyAsync(x => x.Identifier == pKey);
    
                    }
                });
    
                return roomCount;
    
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Naomi N Thursday, June 18, 2020 10:17 PM
    Thursday, June 18, 2020 10:01 PM
  • During debugging in Watch window for Duplicate() gave value description like:
    This expression has side effects and will not be evaluated.

    What need to do to fix it?

    Thanks


    • Edited by zleug Friday, June 19, 2020 12:16 AM
    Friday, June 19, 2020 12:15 AM
  • Can you share your GetDuplicate method code of the EmployeeFactory?

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Friday, June 19, 2020 12:30 AM
  • The problem solved. Trivially was wrong logic in IF statement.

    Thanks. 

    Friday, June 19, 2020 12:54 AM
  • Hi,

    I am glad to hear this news.

    Please click "Mark as Answer" for the reply that solved your problem, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 19, 2020 3:26 AM