如何将列表字符串转换为datainfo对象。 [英] How convert list string into datainfo object.
本文介绍了如何将列表字符串转换为datainfo对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不会将字符串显式转换为DataInfo对象。我想要一个从字符串列表转换为对象的示例,稍后我将用它来创建图形。
我尝试过的:
I wont explicitly convert string into DataInfo object. I am asking for an example of a conversion from the list of strings to an object that will serve me later to create a graph.
What I have tried:
public class DataInfo
{
public string Data { get; set; }
public DataInfo()
{
this.Data = Data;
}
}
//Controller
List<datainfo> DataList = new List<datainfo>();
DataList = db.Order.OrderBy (w => w.IdOrder) .ToList ();
viewModel.ListPeriods = listOrdersUsers.OrderBy (p => p.AcceptanceDateOrder
). Select (w => w.AcceptanceDateOrder.ToString (). Substring (0, 7)) ToList ();
//error
// Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<aplikacjahelpdesk.models.baza.datainfo>
推荐答案
ListPeriods =行上的错误是?是类型列表的ListPeriods< DataInfo>?
如果是这样,如果你看.Select你正在选择一个字符串(w.AcceptanceDateOrder.ToString())所以当你ToList你得到一个List< string>无法分配给List< DataInfo>。所以你的Select需要返回DataInfo,以便ToList返回List< DataInfo>
Is the error on the "ListPeriods =" line? Is ListPeriods of type List<DataInfo>?
If so, if you look at the .Select you are selecting a string (w.AcceptanceDateOrder.ToString()) so when you ToList you get a List<string> which can't be assigned to List<DataInfo>. So your Select needs to return DataInfo so that ToList returns List<DataInfo>
viewModel.ListPeriods = listOrdersUsers.OrderBy (p => p.AcceptanceDateOrder
). Select (w => new DataInfo{Data = w.AcceptanceDateOrder.ToString().Substring(0, 7)}) ToList();
List<DataInfo> DataList = new List<DataInfo>();
DataList = listOrdersUsers.OrderBy<pre>(p => p.AcceptanceDateOrder
). Select (w => new DataInfo{Data = w.AcceptanceDateOrder.ToString().Substring(0, 7)}) ToList();
这篇关于如何将列表字符串转换为datainfo对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文