Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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>
Posted
Updated 2-Feb-18 0:41am
Comments
Richard Deeming 2-Feb-18 10:03am    
You have already posted this question:
https://www.codeproject.com/Questions/1228050/Assign-a-list-of-strings-to-a-propertis-filtering[^]

If you want to update your question, click the green "Improve question" link and edit your question. DO NOT post the update as a new question!

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();
 
Share this answer
 
Comments
przemo27ns 2-Feb-18 6:36am    
Thank you very much for the solution.
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();
 
Share this answer
 
Comments
Richard Deeming 2-Feb-18 10:01am    
Why have you reposted another user's solution? Doing that will get you kicked off the site.
przemo27ns 2-Feb-18 14:35pm    
Sorry this is implementation in my project

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900