I have a URL string coming into an API e.g. c1:1=25
.
*http://mysite/api/controllername?serial=123&c1:=25*
I want to split it into the channel name (c1), the channel reading number (1) after the colon and the value (25).
There are also occasions, where there is no colon as it is a fixed value such as a serial number (serial=123).
I have created a class:
public class UriDataModel
{
public string ChannelName { get; set; }
public string ChannelNumber { get; set; }
public string ChannelValue { get; set; }
}
I am trying to use an IEnumerable with some LINQ and not getting very far.
var querystring = HttpContext.Current.Request.Url.Query;
querystring = querystring.Substring(1);
var urldata = new UrlDataList
{
UrlData = querystring.Split('&').ToList()
};
IEnumerable<UriDataModel> uriData =
from x in urldata.UrlData
let channelname = x.Split(':')
from y in urldata.UrlData
let channelreading = y.Split(':', '=')
from z in urldata.UrlData
let channelvalue = z.Split('=')
select new UriDataModel()
{
ChannelName = channelname[0],
ChannelNumber = channelreading[1],
ChannelValue = channelvalue[2]
};
List<UriDataModel> udm = uriData.ToList();
I feel as if I am over complicating things here.
In summary, I want to split the string into three parts and where there is no colon split it into two.
Any pointers will be great. TIA