Is it possible to convert Int32
to string
?
I am having difficulties with this line of the code, where I am trying to convert Int32
to string
.
var listOrder = daa.OrderByDescending(i => i.Times).ToList().Convert.ToString().OrderByDescending(jtSorting);
i.Times
is Int32
and I have to pass jSorting
parameter which is string.
I get this error when I compile:
"Does not contain a defination for 'Convert' and no extension method 'Convert' accepting a first argument of type"
Full controller code for your inspection:
public JsonResult TopPlayedInVenueList1(string name = "", string ArtistName = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
{
try
{
if (Request.IsAuthenticated == true)
{
string Path = @"C:\\5Newwithdate-records.xls";
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '" +
Path + "';Extended Properties=" + (char)34 +
"Excel 8.0;IMEX=1;" + (char)34 + "");
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
System.Data.DataTable data = new System.Data.DataTable();
da.Fill(data);
con.Close();
List<TopPlayed> daa = new List<TopPlayed>();
foreach (DataRow p in data.Rows)
{
TopPlayed top = new TopPlayed()
{
TrackID = Convert.ToInt32(p.Field<double>("TrackID")),
TrackName = p.Field<string>("TrackName"),
ArtistName = p.Field<string>("ArtistName"),
Times = Convert.ToInt32(p.Field<double>("Times"))
};
daa.Add(top);
}
///// var newlist = daa.OrderByDescending(i => i.Times).ToList().GetRange(jtStartIndex, jtPageSize);
var newlist = daa.AsQueryable().OrderByDescending(jtSorting).OrderByDescending(i => i.Times).ToList();
return Json(new { Result = "OK", Records = newlist, TotalRecordCount = daa.Count });
View Code of jTable Datatable:
$(document).ready(function () {
$('#TopPlayedInVenueContainer1').jtable({
title: 'NSM Biling Report List',
paging: true,
pageSize: 100,
sorting: true,
// defaultSorting: 'Times ASC',
actions: {
listAction: '@Url.Action("TopPlayedInVenueList1")'
},
fields: {
TrackID: {
title: 'Track ID',
key: true,
create: false,
edit: false,
resize: false,
tooltip: 'Track Name'
},
TrackName: {
title: 'Track Name',
key: true,
create: false,
edit: false,
resize: false,
tooltip: 'Track Name'
},
ArtistName: {
title: 'Artist Name',
key: true,
create: false,
edit: false,
resize: false,
tooltip: 'Track Name'
},
Times: {
title: 'Times',
tooltip: 'Artist Name'
}
}
});
$('#LoadRecordsButton').click(function (e) {
e.preventDefault();
$('#TopPlayedInVenueContainer1').jtable('load', {
name: $('#name').val(),
ArtistName: $('#ArtistName').val()
});
});
//Load all records when page is first shown
$('#LoadRecordsButton').click();
});
OrderByExtensions
namespace NSM.BackOffice.Controllers
{
public static class OrderByExtensions
{
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering)
{
var type = typeof(T);
var property = type.GetProperty(ordering);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExp));
return source.Provider.CreateQuery<T>(resultExp);
}
public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string ordering)
{
var type = typeof(T);
var property = type.GetProperty(ordering);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp = Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExp));
return source.Provider.CreateQuery<T>(resultExp);
}
}
}
I am fresh with c# so if you could please let me know a workout to get this to work. thanks in advance.