none
Possibilities new list RRS feed

  • Question

  • Hello,
    ListSingleBoards is a list with objects.
    objects has severals properties.
    I want a new list with one property, a string list.     List<string>
    How can I reach my goal?
    I want this is a better, faster way.
    List<string> newList = new List<string>();
    foreach (var item in qry)
    {
    	newList.Add(item.SerialWrite);
    }
    
    
    //How I can write this as another way?	  
    
    var qry = (from c in ListSingleBoards
    		   where c.Used == false
    		   select new
    		   {
    			   Inhalt = c.SerialWrite
    		   }).ToList();
    	  
    //        List<string> qry = ListBoards.Where(x => x.Used == false).Select(o => new string
    //{
    //            Serial = o.SerialWrite        
    //}).ToList()
    
    
           List<Board> qry2 = (from c in ListSingleBoards
                           where c.Used == false
                           select new 
                           {
                              c.SerialWrite
                           }).ToList();
    
    Thanks for an example.
    Regards Markus


    Thursday, September 27, 2018 4:09 PM

Answers

  • Hi Markus,

    Either one of these syntaxes should work, which is slightly different from yours. It compile fines, but I haven't tested with any data:


    List<string> qry = ListSingleBoards.Where(x => x.Used == false).Select(o => o.SerialToWrite).ToList();
    List<string> qry2 = (from c in ListSingleBoards
                         where c.Used == false
                         select c.SerialToWrite).ToList();

    Oh, and one more thought ... did you remember to instantiate your ListSingleBoards? You probably did, but I didn't see it in your code:

    List<SingleBoard> ListSingleBoards = new List<SingleBoard>();


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, September 29, 2018 4:44 PM
    Moderator
  • //        List<string> qry = ListBoards.Where(x => x.Used == false).Select(o => new string
    //{
    //            Serial = o.SerialWrite        
    //}).ToList()

    How would that look in this spelling and which spelling do you prefer?


    Markus,

    That doesn't compile, and it doesn't make sense anyway. You're creating a list of strings and o.SerialWrite returns you a string, you don't need to do a new string{} command in the middle of it all. There's no way to associate a variable name (Serial) for each string in the List. You can do that afterwards if you needed to (iterating through the List qry and setting Serial = an item in the List)

    I prefer the "." syntax, and it needs to be the way I showed it (no new string anywhere):

    List<string> qry = ListSingleBoards.Where(x => x.Used == false).Select(o => o.SerialToWrite).ToList();



    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, September 30, 2018 4:02 PM
    Moderator

All replies

  • Hi Markus,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about the C# development. Since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, September 28, 2018 6:53 AM
  • Better and faster way is using LINQ to filter your collection. May I know if you get exception/error while running your code provided above?

    Thanks,
    Vivek Bansod
    Blog | MSDN | LinkedIn
     

    Friday, September 28, 2018 7:10 AM
  • Hello Vivek,
    //        List<string> qry = ListSingleBoardsToMES.Where(x => x.Used == false).Select(o => new string
    //{
    //            Serial = o.SerialToWrite        
    //}).ToList()
    I could not compile it.
    I use now this.
    var qry2 = (from c in ListSingleBoards
    			   where c.Used == false
    			   select new 
    			   {
    				 c.SerialToWrite
    			   }).ToList();
    
    	DataAsJson<object>("OnResponseUpdate", qry2);
                  
    	
    	
    public class SingleBoard
    {
    	public enum TypePosition
    	{
    		NotDefine = -1,
    		Single = 1
    	}
    
    	[DataMember(IsRequired = true)]
    	public int Pos { get; set; }
    
    	[DataMember(IsRequired = true)]
    	public int Index { get; set; }
    
    	[DataMember(IsRequired = true)]
    	public int State { get; set; } 
    
    	[DataMember(IsRequired = true)]
    	public bool Used { get; set; } 
    
    	[DataMember(IsRequired = true)]
    	public string SerialToWrite { get; set; }
    	
    	// and more
    }
    			  
    public List<SingleBoard> ListSingleBoards;

    I have a list of objects, they have many properties. Now I only need one of them, of type string.
    The idea is to create a new list of type string. How does it work? What do I have to do?
    Example as from c in ListSingleBoards ...
    Example as ListSingleBoardsToMES.Where(x => x.Used == false).Select
     
    Both ways of writing. Thank you in advance.
    Hope now is clear, what I need.
    Regards Markus


    Saturday, September 29, 2018 10:28 AM
  • Hi Markus,

    Either one of these syntaxes should work, which is slightly different from yours. It compile fines, but I haven't tested with any data:


    List<string> qry = ListSingleBoards.Where(x => x.Used == false).Select(o => o.SerialToWrite).ToList();
    List<string> qry2 = (from c in ListSingleBoards
                         where c.Used == false
                         select c.SerialToWrite).ToList();

    Oh, and one more thought ... did you remember to instantiate your ListSingleBoards? You probably did, but I didn't see it in your code:

    List<SingleBoard> ListSingleBoards = new List<SingleBoard>();


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, September 29, 2018 4:44 PM
    Moderator

  • List<string> qry = ListSingleBoards.Where(x => x.Used == false).Select(o => o.SerialToWrite).ToList();
    List<string> qry2 = (from c in ListSingleBoards
                         where c.Used == false
                         select c.SerialToWrite).ToList();

    Hi Bonnie,

    perfect, thanks.

    //        List<string> qry = ListBoards.Where(x => x.Used == false).Select(o => new string
    //{
    //            Serial = o.SerialWrite        
    //}).ToList()

    How would that look in this spelling and which spelling do you prefer?

    Best regards Markus


    Sunday, September 30, 2018 1:14 PM
  • //        List<string> qry = ListBoards.Where(x => x.Used == false).Select(o => new string
    //{
    //            Serial = o.SerialWrite        
    //}).ToList()

    How would that look in this spelling and which spelling do you prefer?


    Markus,

    That doesn't compile, and it doesn't make sense anyway. You're creating a list of strings and o.SerialWrite returns you a string, you don't need to do a new string{} command in the middle of it all. There's no way to associate a variable name (Serial) for each string in the List. You can do that afterwards if you needed to (iterating through the List qry and setting Serial = an item in the List)

    I prefer the "." syntax, and it needs to be the way I showed it (no new string anywhere):

    List<string> qry = ListSingleBoards.Where(x => x.Used == false).Select(o => o.SerialToWrite).ToList();



    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, September 30, 2018 4:02 PM
    Moderator