Reputation: 2041
I have an issue with LINQ query...
I am retriving data in list
public List<Station_Master> GetAllStations()
{
var stationlist = from s in dc.Station_Masters
select s;
return stationlist.ToList();
}
But here my table Station_Masters contains One Status field as Data Type int..
List will give me all records from table including status...
But I need to show Status as String ...
I mean if Status is 0 then it will return "active" or if it is 1 then it will return "Inactive"
How can I do this with this ?
Upvotes: 1
Views: 1040
Reputation: 1936
I think it's better to keep "active/inactive" description away from data-retrieval logic. As @James suggested - just extend your class and explicitly compile description property.
Upvotes: 0
Reputation: 7448
You need to create a DTO where the Status is a string and not an integer, and leave all other properties as the original object.
Then your method can simply:
return dc.Station_Masters
.Select(c => new CustomObject
{
//Map all other fields and then remap the status
Status = c.Status == 0 ? "Active" : "Inactive"
});
This is however inefficient as if would extract all the rows from the table in order to remap them. You'd better apply the Select
transformation only after filtering them.
Upvotes: 0
Reputation: 82136
There are quite a number of ways you could do this, one example could be to extend the Station_Master
class to expose a property that will return you the string representation of the status:
public partial class Station_Master
{
public string StatusText
{
get
{
switch (Status)
{
case 0:
return "Active";
case 1:
return "Inactive";
default:
return "Unknown";
}
}
}
}
Upvotes: 7