How to cast type in a generic class

Mr Edge 221 Reputation points
2022-02-21T14:14:21.717+00:00

I have a table which contains a field with the same name in other similar tables. I then have similar code as below in a Generic class so it could be re-used for other tables.

    public void CompareValueTexture(T t, IQueryable<T> getData)
    {
        foreach (var i in getData)
        {
            if (i.FieldName == anothervalue)
            {
                .....
            }
        }
    }

My plan was to have one Generic class as i need to do the same with other tables too but the issue i have here is the code i.FieldName cant be casted to the Type in a Generic class as it wont work with other tables when i getData for that table. Is there a better way to do this or would it make sense just to repeat the code for each table?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,401 Reputation points
    2022-02-21T15:13:19.537+00:00

    Perhaps this may work for you

    public interface IBase
    {
        public int Id { get; }
    }
    
    public class SomeClass
    {
        public static void CompareValueTexture<T>(IQueryable<T> sender)
        {
            foreach (var item in sender)
            {
                if (item is IBase itemData)
                {
                    Debug.WriteLine(itemData.Id);
                }
            }
        }
    
        public static void CompareValueTexture<T>(List<T> sender)
        {
            foreach (var item in sender)
            {
                if (item is IBase itemData)
                {
                    Debug.WriteLine(itemData.Id);
                }
            }
        }
    }
    
    0 comments No comments

  2. AgaveJoe 28,031 Reputation points
    2022-02-21T15:26:21.147+00:00

    I don't understand the use case but reflection is required to get the property name.

    string columnName = "Id";
    IQueryable<Category>? query = _context.Category.Where(c => c.Id > 0);
    foreach(Category item in query)
    {
        if(item.GetType().GetProperties().Where(p => p.Name == columnName).Any())
        {
           //The column exists 
        }
    }
    

    Usually when several tables have the same column name, these columns are used to join the tables. Maybe if you explain the general idea, we can provide a better approach.

    0 comments No comments

  3. Viorel 116.6K Reputation points
    2022-02-21T17:23:56.487+00:00

    Consider this approach too:

    foreach( dynamic i in getData )

    0 comments No comments

  4. Mr Edge 221 Reputation points
    2022-02-22T09:25:46.913+00:00

    To give an example consider the two tables below

    Category
    Id, Name, Description, AdvertWeight

    Product
    Id, Name, Description, AdvertWeight

    AdvertWeight contains an integer value in both tables to dictate which category or product has the highest value and that category/product would display more frequently in relevant systems.

    The admin team could come in one day and change the weight value and raise the value for the category/product. When a category/product weight is changed the user can press the up/down key which will move the current category/product AdvertWeight up or down in value.

    Considering this i have other tables with the same field and functionality which i wanted to avoid copying and pasting the same code and changing the Type for each class so thought to try and create a generic class but the obstacle comes when i need to compare the current AdvertWeight value which is a field name in the table.

    I will have a go at the solutions proposed above.

    Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.