Search code examples
c#linq

"Sequence contains no element" when it actually find one


I have actually all working around :

  • my connection work well
  • my table is getted
  • my field is founded into my class
  • I get my mac address from my item to compare it

But when I continue my debug the LINQ code go into the "catch Exception" part and gave me this error... Why ?

Here the Debug screen shot : Screen shot : code + debug

I don't know why the sample doesn't return the error but with bool comparison I get the "No way, I can't compare object with bool" which is more explicit than "Sequence contains no element".

Here the sample (if you have a different way to show the code, please let me now, this is my first Stack overflow post :B ) :

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;

namespace Exemple
{
    public class LocalDTB
    {
        public static ConcurrentDictionary<string, MyEntity> MyEntityTab = new ConcurrentDictionary<string, MyEntity>();
    }

    public class InheritedClass
    {
        public long Id { get; protected set; }
    }

    public class MyEntity : InheritedClass
    {
        public string Name;
        public string MacAddress;
        public bool IsIncredible;
        public DateTime LastConnection;

        public MyEntity(long id, string name, string macAddress, bool isIncredible, DateTime lastConnection)
        {
            Id = id;
            Name = name;
            MacAddress = macAddress;
            IsIncredible = isIncredible;
            LastConnection = lastConnection;
        }

        public static ConcurrentDictionary<string, MyEntity> GetAllLocal() => LocalDTB.MyEntityTab;
    }

    public class Program
    {
        public static void Main()
        {
            MyEntity element1 = new MyEntity(10, "ghuilobiukg", "E5:AB:4C:D3:24:68", true, new DateTime());
            MyEntity element2 = new MyEntity(5422, "qsdlkjfhmsssszzzz", "F1:7D:89:3E:AB:87", false, new DateTime());

            ConcurrentDictionary<string, MyEntity> myTab = GetLocalTable<MyEntity>();
            myTab.GetOrAdd(element1.Id.ToString(), element1);
            myTab.GetOrAdd(element2.Id.ToString(), element2);

            Console.Write("Searching element with name : ghuilobiukg\n");
            MyEntity elementFound = GetOneLocal<MyEntity>("Name", "ghuilobiukg");
            if (elementFound != null)
            {
                Console.Write("Element found with name : " + elementFound.Name + "\n");
            }

            Console.Write("Searching element with macAddress : F1:7D:89:3E:AB:87\n");
            elementFound = GetOneLocal<MyEntity>("MacAddress", "F1:7D:89:3E:AB:87");
            if (elementFound != null)
            {
                Console.Write("Element found with name : " + elementFound.Name + "\n");
            }

            Console.Write("Searching element with isIncredible : true\n");
            elementFound = GetOneLocal<MyEntity>("IsIncredible", true);
            if (elementFound != null)
            {
                Console.Write("Element found with isIncredible : " + elementFound.IsIncredible + "\n");
            }
        }

        public static ConcurrentDictionary<string, T> GetLocalTable<T>()
        {
            MethodInfo getAllLocal = typeof(T).GetMethod("GetAllLocal");
            if (getAllLocal == null)
            {
                Console.Write("Method GetAllLocal not found into " + typeof(T).Name + "\n");
                return new ConcurrentDictionary<string, T>();
            }
            else
                return (ConcurrentDictionary<string, T>)getAllLocal.Invoke(null, null);
        }

        public static T GetOneLocal<T>(string fieldName, dynamic value)
            where T : InheritedClass
        {
            FieldInfo[] fields = typeof(T).GetFields(); // Just to see what you can get
            FieldInfo fieldFound = typeof(T).GetField(fieldName);
            if (fieldFound == null)
            {
                Console.Write("field not found into class\n");
                return null;
            }

            try
            {
                return (from Item in GetLocalTable<T>()
                        where fieldFound.GetValue(Item.Value) == value
                        select Item.Value).First();
            }
            catch (Exception e)
            {
                Console.Write("value not found with code error : " + e.Message + "\n");
                return null;
            }
        }
    }
}

Solution

  • Ok a simple cast into dynamic fix it (-_-')

     return (from Item in GetLocalTable<T>()
             where (dynamic)fieldFound.GetValue(Item.Value) == value
             select Item.Value).First();
    

    UPDATE - 18/07/2022

    Solution above work but it's better to go that way :

     return (from Item in GetLocalTable<T>()
             where fieldFound.GetValue(Item.Value).Equals(value)
             select Item.Value).First();