Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

C#: How to check if LIST<T> item exists and sortting

Following on from @ gr8gonzo  comment here on serialize and deserialize XML

This will test if a show exist before adding it

            if(tv.Shows.Exists(show => show.TMBDBid == 77236))
            {
                Console.WriteLine("A Discovery Of Witches Exists");
            }
            else
            {
                tv.Shows.Add(new Show()
                {
                    Name = "A Discovery Of Witches",
                    FolderName = "A Discovery Of Witches",
                    TMBDBid = 77236



                });
            }

Open in new window


How do I test if an episode exists

Tried this but both produced errors so didn't  continue  with  Episode.EpisodeNumber etc
if (tv.Shows.Exists(show => show.TMBDBid == 67133 && show.Season.SeasonNumber))

// AND this
if (tv.Shows.Exists(show => show.TMBDBid == 67133 && show.Season => Season.SeasonNumber))

Open in new window


Again expanding on the example  How do I list the episodes?

         foreach (Show show in tv.Shows)
            {
                // Print out what TV shows are listed

                string ShowName = show.Name;
                string ShowID = show.TMBDBid.ToString();
                Console.WriteLine("ShowName: {0}, TMBDBid: {1} ", ShowName, ShowID);
                foreach (Season season in show.Season)
                {
                    String SeasonNumber = season.SeasonNumber.ToString();
                    Console.WriteLine("SeasonNumber: {0}", SeasonNumber);
                }
                foreach(Episode episode in show.season.)
                {
                    // How do I list the episodes?
                   //                Error	CS1061	'Show' does not contain a definition for 'season' and no extension method 'season' accepting a first argument of type 'Show' could be found
                }
            }

Open in new window


Also when adding a new show or episode can you add it in order of show name then SeasonNumber and finally EpisodeNumber
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

if (tv.Shows.Where(show => show.TMBDBid == 67133).FirstOrDefault() != null)

Open in new window

Avatar of trevor1940
trevor1940

ASKER

That looks better method for the  existence of a show but what about an episode?
C# is case sensitive.  Your class has Season as S vs s


For episodes given a season:
  Season s = show.Seaon.Where( ss => ss.SeasonNumber == "3").FirstOrDefault();
  
  if (s != null)
  {
    foreach(Episode episode in s)
    {
     // do something
    }
  }
   else 
{
  // no info for that season number.
 } 

Open in new window



or you could do

for each show
    for each season
       for each episode
So to check if an episode exists before adding a new one I have to do something like this?

if (tv.Shows.Where(show => show.TMBDBid == 67133).FirstOrDefault() != null)
{
  // check for season
  Season s = show.Seaon.Where( ss => ss.SeasonNumber == "3").FirstOrDefault();
  
  if (s != null)
  {
    Episode e = s.Episode.Where(ee => ee.EpisodeNumber == "1").FirstOrDefault();
        if(e == null)
    {
     // Add new episode
    }
  }
   else 
{
  // no info for that season number.
 } 
}

Open in new window

I tried the code above I get
The name 'show' does not exist in the current context

Open in new window


Can some one explain what I'm doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
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
Hi Kyle

I corrected some typo's

There is no "Where" @
Episode e = s.Episode.Where

Open in new window

see pic
User generated image
s.Episodes.Where
Hi Kyle

There is no s.Episodes.Where  either

Should there be?


Changing
    public class Season
    {
        public int SeasonNumber { get; set; }
        public Episode Episode { get; set; }
    }

Open in new window

To
    public class Season
    {
        public int SeasonNumber { get; set; }
        public List<Episode> Episode { get; set; } = new List<Episode>();
    }

Open in new window


Worked But This meant the resulting XML looks like this

      <Season>
        <Season>
          <SeasonNumber>3</SeasonNumber>
          <Episode>
            <Episode>
                ... etc
            </Episode>
          </Episode>
        </Season>
      </Season>

Open in new window

It would be nice if it could be

      <Seasons>
        <Season>
          <SeasonNumber>3</SeasonNumber>
          <Episodes>
            <Episode>
                ... etc
            </Episode>
          </Episodes>
        </Season>
      </Seasons>

Open in new window

I'm unsure how to achieve this other than add more classes  ? ?

    public class Season
    {
        public int SeasonNumber { get; set; }
        public List<Episodes> Episodes { get; set; }

    }

    public class Episodes
    {
        public List<Episode> Episode { get; set; } = new List<Episode>();
    }

    public class Episode
    {
        public int EpisodeNumber { get; set; }
        public string EpisodeName { get; set; }
        public string BackDrop { get; set; }
        public string FullPath { get; set; }
    }

Open in new window

No need for the Episodes class.  What you name it is what it becomes:

 public List<Episode> Episodes { get; set; } = new List<Episode>();

Open in new window


a List of Type Episode is named Episodes.  Try that and see what you get (remove the Episodes class).

Just think about it from a Object standpoint.

You have a show
a show has seasons . . . what is seasons?  a List of Season
Each season has episodes, being a list of Episode

etc
Thank you for your help and patience