是否可以将Int32转换为String?

时间:2014-04-02 09:31:34

标签: c# asp.net-mvc-4

是否可以convert Int32string

我在使用这一行代码时遇到了问题,我尝试将Int32转换为string

var listOrder = daa.OrderByDescending(i => i.Times).ToList().Convert.ToString().OrderByDescending(jtSorting);

i.TimesInt32,我必须传递jSorting参数,即字符串。

编译时出现此错误:

“不包含'转换'的定义,也没有扩展方法'转换'接受类型为”

的第一个参数

完整的控制器代码供您检查:

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 });

查看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);
    }
}

}

我对c#感到很新鲜,所以如果可以的话请让我知道一个锻炼来让这个工作。提前谢谢。

6 个答案:

答案 0 :(得分:3)

您可以在Enumerable.Select

投影期间致电ToString
var listOrder = daa.OrderByDescending(i => i.Times).Select(c=>c.Times.ToString()).ToList();

答案 1 :(得分:2)

这部分:

OrderByDescending(i => i.Times).ToList().Convert.ToString()
看起来很奇怪。 Convert这里不是属性而不是方法调用。您可能想要这样:

OrderByDescending(i => i.Times).ToList().Select(x => x.ToString())

更新。跟进评论中的讨论 - 以下是如何使用已实施的扩展方法here。在项目的某处声明一个静态类,如下所示:

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);
    }
}

确保此类在TopPlayedInVenueList1内可见,如果不是,请使用using进行此类操作。之后,您应该可以使用任何IQueryable<>

执行此操作
.OrderByDescending(jtSorting);

答案 2 :(得分:2)

你的jsSorting参数用于排序数据表,而不是IEnumerables: 试试这个:

data.DefaultView.Sort = jsSorting;   

答案 3 :(得分:0)

您正在尝试将列表对象转换为字符串:

var listOrder = daa.OrderByDescending(i => i.Times).ToList().Convert.ToString();

你可能想要什么(我不知道你的要求是):

var listOrder = daa.OrderByDescending(i => i.Times).Select(i => i.Times.ToString()).ToList();

哪个会给你一个字符串列表。


根据您的评论,如果指出的技术超出您的范围,您可以这样做(如果您要排序的列数有限,则可能没问题):

List<TopPlayed> sorted;

switch (jtSorting)
{
   case "Times":
      sorted = daa.OrderByDescending(i => i.Times).ToList();
      break;
   case "Other":
      sorted = daa.OrderByDescending(i => i.Other).ToList();
      break;
    default:
      //Oops.  Do something appropriate here.
}

// etc.

答案 4 :(得分:-1)

C#中的所有内容都来自object,它具有ToString()方法。并非所有东西都提供了特别有用的实现(有些东西只是打印它们的类型名称,特别是集合),但Int32可以。因此,i.Times.ToString()会为您提供字符串版本。

答案 5 :(得分:-1)

如果您使用Linq

daa.OrderByDescending(i => i.Times.ToString())
daa.OrderByDescending(i => (string)i.Times)

如果你在EF上使用Linq

daa.OrderByDescending(i => SqlFunctions.StringFormat((double?)i.Times))