Link to home
Start Free TrialLog in
Avatar of ghj Grandhe
ghj Grandhe

asked on

Need to retrieve values from list and assign value it to specific property

In c#, For Example the list contains {Eid:67,EName:'dyt',DOB:06/09/2017,Sal:6078}.
Actually i declared a class with properties as Eid,Ename,DOB,Sal etc...
 my question is from list i need to retrieve values and need to assign it to specific property.
For ex: Eid property need to assign 67.
like that for all properties in c#.

could you please help
Avatar of Bill Prew
Bill Prew

Can you share the code you used to create the List, and the class(es) involved?


»bp
The way you present your list make's it seem as if it is not a list but rather a JSON Object.  If this is the case, then you can use a library like Newtonsoft's JSON.NET to deserialize your object into the class object you specified; e.g. -
using Newtonsoft.Json;
using System;
using System.Linq;

namespace EE_Q29123999
{
	class Program
	{
		static void Main(string[] args)
		{
			var data = "{\"Eid\":67, \"EName\":\"dyt\", \"DOB\":\"06/09/2017\", \"Sal\":6078}";
			var converted = JsonConvert.DeserializeObject<Record>(data);
			Console.WriteLine(converted);
			Console.ReadLine();
		}
	}

	class Record
	{
		public int Eid { get; set; }
		public string EName { get; set; }
		public DateTime DOB { get; set; }
		public decimal Sal { get; set; }

		public override string ToString()
		{
			return $"{{ {string.Join(", ", from prop in GetType().GetProperties() select $"{prop.Name}: {prop.GetValue(this, null)}")} }}";
		}
	}
}

Open in new window

Which produces the following output -User generated imageIf, however, it is not a JSON Object, then as Bill Prew stated, we will need more specifics about the creation of your list.

-saige-
Here you go. I created a simple windows form application to test it out. When a button on the form is clicked, the method button1_Click is called and your list is created, populated, and then iterated through. I hope this helps. :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public class ColumnData
{
    public int Eid { get; set; }
    public string EName { get; set; }
    public string DOB { get; set; }
}
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<ColumnData> DataItems = new List<ColumnData>();
            ColumnData myFirstRecord = new ColumnData();

            myFirstRecord.Eid = 67;
            myFirstRecord.EName = "Sal";
            myFirstRecord.DOB = "06/09/2017";

            DataItems.Add(myFirstRecord);

            ColumnData SecondRecord = new ColumnData();

            SecondRecord.Eid = 31;
            SecondRecord.EName = "Aair";
            SecondRecord.DOB = "07/09/1992";

            DataItems.Add(SecondRecord);

            int testEid = 0;
            string testName = string.Empty;
            string testDob = string.Empty;

            foreach (ColumnData obj in DataItems)
            {
                testEid = obj.Eid;
                testName = obj.EName;
                testDob = obj.DOB;
            }
        }
    }
}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.