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
How do I test if an episode exists
Tried this but both produced errors so didn't continue with Episode.EpisodeNumber etc
Again expanding on the example How do I list the episodes?
Also when adding a new show or episode can you add it in order of show name then SeasonNumber and finally EpisodeNumber
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
});
}
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))
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
}
}
Also when adding a new show or episode can you add it in order of show name then SeasonNumber and finally EpisodeNumber
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:
or you could do
for each show
for each season
for each episode
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.
}
or you could do
for each show
for each season
for each episode
ASKER
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.
}
}
ASKER
I tried the code above I get
Can some one explain what I'm doing wrong?
The name 'show' does not exist in the current context
Can some one explain what I'm doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
s.Episodes.Where
ASKER
Hi Kyle
There is no s.Episodes.Where either
Should there be?
Changing
Worked But This meant the resulting XML looks like this
There is no s.Episodes.Where either
Should there be?
Changing
public class Season
{
public int SeasonNumber { get; set; }
public Episode Episode { get; set; }
}
To public class Season
{
public int SeasonNumber { get; set; }
public List<Episode> Episode { get; set; } = new List<Episode>();
}
Worked But This meant the resulting XML looks like this
<Season>
<Season>
<SeasonNumber>3</SeasonNumber>
<Episode>
<Episode>
... etc
</Episode>
</Episode>
</Season>
</Season>
It would be nice if it could be <Seasons>
<Season>
<SeasonNumber>3</SeasonNumber>
<Episodes>
<Episode>
... etc
</Episode>
</Episodes>
</Season>
</Seasons>
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; }
}
No need for the Episodes class. What you name it is what it becomes:
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
public List<Episode> Episodes { get; set; } = new List<Episode>();
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
ASKER
Thank you for your help and patience
Open in new window