Link to home
Start Free TrialLog in
Avatar of Stacie
StacieFlag for United States of America

asked on

C# syntax on a select ... x.StationActivity.Select( x => x.ProductsUsed.ToString());

I have an object that I'm trying to return the following value but I'm having some issue with my syntax... my object x contain a property StationActivity that also contain a set of 8 more property that I would like to access let say productUsed that is part of the stationActivity.  Please see screen attached. What is the best way to do this?

My Problem is I have this following model, I can get access to the customerNumber but how can I get access to the StationActivity property list object that is of type StationHistoryReportActivity



public class StationHistoryReportResults : CustomerStations
    {
        public StationHistoryReportResults(CustomerStations parentObj)
        {
            
      
        public int CustomerNumber { get; set; }


        public List<StationHistoryReportActivity> StationActivity { get; set; }
    }

Open in new window


   
  foreach (var x in (List<StationHistoryReportResults>)TempData["excsv"])

                {
                    var data = String.Join(",", new string[] {
                  
                        x.CustomerNumber.ToString()?? string.Empty,
                   
                        x.StationActivity.Select( x => x.ProductsUsed.ToString());

Open in new window


User generated image
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have a look at "SelectMany"
Avatar of Stacie

ASKER

Still having some issue how I can syntactically write this...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stacie

ASKER

This is the wrong syntax. My ProductUsed containg many product code that I'm trying to reach...

      x.StationName.ToString(),});

                  
                    //string.Format("\"{0}\"",x.StationActivityComments)?? string.Empty,


                    foreach (StationHistoryReportActivity shra in x.StationActivity)
                    {
                        shra.ProductsUsed.SelectMany(x => x.ProductCode);
;                   }

Open in new window

Avatar of Stacie

ASKER

Now I'm getting the following error. System.Collections.Generic.List`1 when the value is exported on  String.Join(",", x.

    foreach (var x in (List<StationHistoryReportResults>)TempData["excsv"])

                {
                       var data = String.Join(",", new string[] {
                      
                        x.StationName.ToString(),

                      String.Join(",", x.StationActivity.Select(x1 => x1.ProductsUsed.ToString()).ToList()),



                });

Open in new window

Avatar of Stacie

ASKER

My problem is my ProductUsed contain another set of property that I'm trying to get to...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stacie

ASKER

Here is my model for a better explanation. My goal is to get to all the property of StationProductUsed  

 public class StationHistoryReportResults : CustomerStations
    {

        public int ServiceRoundId { get; set; }
        public int InvoiceNumber { get; set; }
        public List<StationHistoryReportActivity> StationActivity { get; set; }
    }  

     public class StationHistoryReportActivity
         {
        public int StationId { get; set; }
        public List<StationProductsUsed> ProductsUsed { get; set; }
     
         }


   public class StationProductsUsed
    {
        public int StationActivityId { get; set; }
        public int StationId { get; set; }
        public string ProductCode { get; set; }
        public string ProductTranslatedDescription { get; set; }
        public string Target { get; set; }
        public string Comments { get; set; }
        public string DoneBy { get; set; }
    }