1

This is my api in json format

[{"tu_id":1,"tu_name":"Akhil","tu_usrname":"Akhl","tu_pwd":"1234","tu_cntno":"423433"}]

I want ID instead of tu_id and Name instead of tu_name

this is my code in controller

public IEnumerable<TestUser> Get()
    {
        DataTable dt = new DataTable();
        TestUserBL userbl = new TestUserBL();
        dt = userbl.TestUserSel();
        var user = dt.AsEnumerable().Select(x => new TestUser
        {


              tu_id = x.Field<int>("tu_id"),
              tu_name = x.Field<string>("tu_name"),
              tu_usrname=x.Field<string>("tu_usrname"),
              tu_pwd=x.Field<string>("tu_pwd"),
              tu_cntno=x.Field<string>("tu_cntno")
            // like wise initialize properties here
        });

        return user;
    }

Here I cant change tu_id?

Aju
  • 503
  • 1
  • 8
  • 26
Akhil George
  • 17
  • 1
  • 3
  • please follow coding conventions in your code as it matters a lot in case of clean code https://stackoverflow.com/a/1618325/5621827 – jitender Jun 02 '17 at 11:02

2 Answers2

1

Change property names(tu_id to ID and tu_name to Name etc) in TestUser class they don't follow the conventions conventions then do mapping as follow:-

 var user = dt.AsEnumerable().Select(x => new TestUser
        {

              Id= x.Field<int>("tu_id"),
              Name = x.Field<string>("tu_name")
        });
jitender
  • 10,238
  • 1
  • 18
  • 44
1

You can use serialization attributes applied to properties of your TestUser class:

 [JsonProperty("ID")]
 public int tu_id { get; set; }

 [JsonProperty("Name")]
 public string tu_name { get; set; }
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459