2

I am using dotConnect for MySQL product of Devart. MySQL database structure likes this:

enter image description here

I am getting data like this:

public int user_id { get; set; } = 2;
public string lang { get; set; } = "en"; // Depending on the situation, it may also be "tr".
private readonly mainDataContext _db = new();

var cats = _db.categories.Where(s => s.u_id == user_id);
foreach (var cat in cats)
{
    MessageBox.Show(cat.name_en);
}

In the MessageBox.Show I can not use cat.name + "_" + lang like PHP. I don't know how to get over this problem.

Johnny Wind
  • 151
  • 8
  • 1
    Does this answer your question? [Get property value from string using reflection](https://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection) – Svyatoslav Danyliv Nov 30 '22 at 17:26
  • @SvyatoslavDanyliv - Firstly thank you very much. It looks awesome. But in Turkish, there is a multibyte character problem. For example, `Snacks` in Turkish meaning is `Atıştırmalıklar`. But it returns in this samples like `At??t?rmal?klar`. So `ı`, `ş`, `İ` ext. are `?`. How can I managed it? – Johnny Wind Nov 30 '22 at 20:47
  • If translation is stored in Unicode, probably you have just problem with fonts on frontend. – Svyatoslav Danyliv Nov 30 '22 at 21:02
  • @SvyatoslavDanyliv - Yes, sir. It seems just you say. But I don't know the solution, about of my limited knowledge. And using `Charset=utf8` did not solve the problem. – Johnny Wind Nov 30 '22 at 21:10
  • Now it is ok. When I texted `Charset=utf8` after database name in the connection string, it corrected. Thank you very much. – Johnny Wind Nov 30 '22 at 21:19

1 Answers1

2

In nutshell, you can use this:

cat.GetType().GetProperty("name_" + lang).GetValue(cat,null))

But it's better to call a method to get value:

static public T getval<T>(Object obj, string field)
        {
            return (T)obj.GetType().GetProperty(field).GetValue(obj, null);
        }

Here is a full example:

using System;

namespace Example
{
    public class user
    {
        public int user_id { get; set; } = 2;
        public string name_en { get; set; }
        public string name_tr { get; set; }

       
    }
    
    class Program
    {
        static public T getval<T>(Object obj, string field)
        {
            return (T)obj.GetType().GetProperty(field).GetValue(obj, null);
        }
        static void Main(string[] args)
        {

            List<user> u = new List<user>();
            u.Add(new user { user_id = 1, name_en = "Foods", name_tr = "name_tr value 1" });
            u.Add(new user { user_id = 2, name_en = "Pizza", name_tr = "name_tr value 2" });
            u.Add(new user { user_id = 2, name_en = "Other", name_tr = "name_tr vale 3" });
            var lang = "en";
            var cats = u.Where(s => s.user_id == 2);
            foreach (var cat in cats)
            {
                Console.WriteLine(getval<string>(cat,"name_"+lang));
            }
            return;
        }
    }
}
mahdi gholami
  • 482
  • 1
  • 3
  • 13
  • Firstly thank you very much. It looks awesome. But in Turkish, there is a multibyte character problem. For example, `Snacks` in Turkish meaning is `Atıştırmalıklar`. But it returns in your code like `At??t?rmal?klar`. So `ı`, `ş`, `İ` ext. are `?`. How can I managed it? – Johnny Wind Nov 30 '22 at 20:43
  • If an encoding issue occurs with data read from the database, try adding "Unicode=true;" to your connection string. – Devart Feb 13 '23 at 12:33