Handling many times the same operation - c#

I'm looking for a way to "divide" multiple operations that needs to be done so as to gain efficiency.
Let me put an example to explain my need.
Say that I have this object:
public class CardInfo
{
public int mCardID { get; set; }
public string mCardName { get; set; }
public int? mCardNumber { get; set; }
public string mCardColor { get; set; }
(...)
}
Each objects may be linked to a price list object:
public class PriceListInfo
{
public int mPriceListId { get; set; }
public int mPriceProviderID { get; set; }
public PriceProviderInfo mPriceListProvider { get; set; }
public int mCardID { get; set; }
public CardInfo mCard { get; set; }
public decimal? mPriceLow { get; set; }
public decimal? mPriceMid { get; set; }
public decimal? mPriceHigh { get; set; }
}
Now I have a process which task is to actually get a list of cards and build the price list of each cards, like this:
List<CardInfo> listCards = mCardManager.ListCardsForCardSet(_cardSetID);
foreach(var card in listCards)
{
List<PriceListInfo> listPrices = mPriceManager.ListPricesForCar(card);
}
Something quite simple, however if the list of cards contains more than 500 items, it will take a while. I am wondering (and since I don't know much about Asp.Net, it is why I am asking the question) if there's a way to divide the list by an [X] number and then launch many times the List<PriceListInfo> listPrices = mPriceManager.ListPricesForCar(card); line? Or is there a way to make these operation asynchronously? (please pardon my ignorance, thank you.) Can anybody explain how could I proceed, and if you could provide an example, I'd be more than happy.

Related

.net6 dynamic option choices with Lists?

I am building a character creator for an out-of-print TTRPG system. The issue that I've run into is how to dynamically create dropdown boxes where the user can make skill/weapon choices based on their race/class choice.
For example: If user picks raceOne, he may pick any Lore skill he wants(he can do this twice), and he may choose one of any profession.
Now, Race two has some more interesting choices: He needs to pick between skillId 5 or SkillId 6, he also needs to make a decision between skillId 7 or 8.
My initial thought was to create a multi-dimensional array, and have array[x][] represent the number of choices one would have to make (RaceTwo in this example, has 2 different choices to make.) and choice[0][x] would represent one of the options. and then in my view I could just iterate through the array to create the dropdowns and their options, but it appears that arrays cannot be sized dynamically in c#.
Attached here is my RaceEntity, and the StartingSKills entity. I dont know enough about what I'm doing to know exactly what all code I should post here, but I'm trying to learn.
public class Race
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string? ImagePath { get; set; }
public virtual IList<RaceStartingArmor> RaceStartingArmor { get; set; }
public virtual IList<RaceStartingSkills> RaceStartingSkills { get; set; }
public virtual IList<RaceStartingTalents> RaceStartingTalents { get; set; }
public virtual IList<RaceStartingWeapons> RaceStartingWeapons { get; set; }
public virtual IList<RaceStartingGear> RaceStartingGear { get; set; }
}
public class RaceStartingSkills
{
public RaceStartingSkills(int RaceId, int SkillId , bool isKnown)
{
this.RaceId = RaceId;
this.SkillId = SkillId;
this.isKnown = isKnown;
}
public int Id { get; set; }
[ForeignKey(nameof(Race))]
public int RaceId { get; set; }
public Race Race { get; set; }
[ForeignKey(nameof(Skill))]
public int SkillId { get; set; }
public Skill Skill { get; set; }
public bool isKnown { get; set; }
public bool isTrained { get; set; }
public bool isSkilled { get; set; }
public bool isExpert { get; set; }
}
some more context: After race/class is chosen, the user is taken to a view where both race and class options are to be populated and chosen, this DTO will then be sent to the character sheet view and populated
any information, or video links, or anything just to get me moving in the right direction would be exceptionally helpful.

C# DTO List that consists of 2 other lists

Firstly, apologies if this seems basic, I am new to C#/dotnet and if the answer to this questions is somewhere obvious please point me in the right direction.
I have a DTO class with the following code
public class LessonDetailView : BaseResult
{
public long Id { get; set; }
public string Title { get; set; }
public List<LessonImagesListView> LessonImages { get; set; }
public List<LessonInstructionCardListView> InstructionCards { get; set; }
}
public class LessonImagesListView
{
public long Id { get; set; }
public string Title { get; set; }
public ImageDetailView Image { get; set; }
public LessonImagesListView()
{
Image = new ImageDetailView();
}
}
public class LessonInstructionCardListView
{
public long Id { get; set; }
public string Instructions { get; set; }
}
So I have 2 distinct types of object that I attach to the lesson and send to the frontend.
I will add that in the future I might have 6 different types of object.
These Images, or Instructions are also going to be displayed in a certain order on the front end so instead of sending them all separately I wanted to combine them all and send them in a new List LessonAssetsListView for example.
How can i create Lists in a DTO that combine 2 other lists ?
OR ... is this something I even need to do here ... and can i just do all this in my service.
Help appreciated.
You could simply define a type that composes both your existing and send a List of them
public class LessonAsset
{
public LessonImagesListView Image {get;set; }
public LessonInstructionCardListView Instruction {get;set;}
}
and then
public class LessonDetailView : BaseResult
{
public long Id { get; set; }
public string Title { get; set; }
public List<LessonAsset> LessonAssets { get; set; }
}

Which design pattern will be best for football match app

I am creating web api for application which support football team in collecting statistic data from matches. I am on implementing phase now. And lets say I am thinking what (if it is needed) type of design patter will be best for something like this:
public class Shot
{
public int Id { get; set; }
public int PlayerId { get; set; }
public string Comment { get; set; }
public bool OnGoal { get; set; }
public int GameId { get; set; }
}
and
public class Card
{
public int Id { get; set; }
public int PlayerId { get; set; }
public string Comment { get; set; }
public bool IsRed{ get; set; }
public int GameId { get; set; }
}
As You can see some properties are same. It should be implemented with interface, inheritance ( f.e. class Action) or maybe I should use one of Design patterns (which one)? What will be best for Entity Framework to avoid problems in later phases?
Well, both your classes represent some kind of game event - shot and card. There could be some other game events, like a free kick, throw in, substitution, penalty, or corner kick. All of these events should have id, game id, player id, timestamp, and probably comment. So your problem is the duplication of data in several classes. It is easily solved with inheritance. No patterns required:
public abstract class GameEvent
{
public int Id { get; set; }
public int GameId { get; set; }
public int PlayerId { get; set; }
public TimeSpan Time { get; set; }
public string Comment { get; set; }
}
And various specific events
public class Shot : GameEvent
{
public bool OnGoal { get; set; }
}
public class Card : GameEvent
{
public bool IsRed { get; set; }
}
You should also think about saving timestamp of added time because you can get both 46 minutes time span (start of second half) and 45+1 minutes of first half.

Object optimization and grouping

I have objects that are defined this way:
class BFull
{
public string ImageID { get; set; }
public List<string> Tapes { get; set; }
}
class OptSet
{
public int SetID { get; set; }
public List<string> Tapes { get; set; }
public List<string> Images { get; set; }
}
The BFull object is DB defined and I can't alter it much. The second one is the wanted result of my actions. And the problem is:
I need to optimize those BFulls into OptSets with a limitation that any given OptSet can have max 24 distinct tapes and consist only complete BFulls. Can anybody help how to achieve that?

WP8 JSON Deserialisation of Arrays nested in Arrays nested in Arrays

I want to do something quite simple.
I just want to take a JSON string (which I have) and populate a whole bunch of stuff with it.
The problem for me is that there are arrays hidden in array inside more arrays, and I can't get at my data.
I tried standard deserialization like so ...
var apiData = JsonConvert.DeserializeObject<RootObject>(json);
But this only lets me get into the top layer - what is in rootObject
I tried making a dictionary ...
Dictionary<string, dynamic> values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json);
but that doesn't let me drill down either (or I can't make it).
I have set up my c# using json2c#
I've been all over the internet including here.
The closest I have got to creating anything close is an expandoObject
var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);
which I can see from the debugger is keeping everything structured as I would like but I've no idea how to get some of the stuff out.
Long story short, its been two days now and I'm bashing my head against a brick wall.
I just want to be able to get the data out of the JSON string and I can't change that string as its not mine.
public class NewsArticlesList
{
public string title { get; set; }
public string link { get; set; }
public string source { get; set; }
public string snippet { get; set; }
}
public class jobsList
{
public string title { get; set; }
public string titleLinkUrl { get; set; }
public List<object> relatedSearchesList { get; set; }
public string formattedTraffic { get; set; }
public int trafficBucketLowerBound { get; set; }
public int interestLevel { get; set; }
public string interestColor { get; set; }
public List<NewsArticlesList> newsArticlesList { get; set; }
public double startTime { get; set; }
public string shareUrl { get; set; }
public string date { get; set; }
}
public class jobsByDateList
{
public string date { get; set; }
public string formattedDate { get; set; }
public List<jobsList> jobsList { get; set; }
}
public class RootObject
{
public string summaryMessage { get; set; }
public double dataUpdateTime { get; set; }
public List<jobsByDateList> jobsByDateList { get; set; }
public string oldestVisibleDate { get; set; }
public bool lastPage { get; set; }
}
My problem is these lists inside lists inside lists.
I can get to jobsListByDate.formattedDate but I can't get anywhere near jobsListByDate.jobsList.titleLinkUrl let alone inside the NewsArticlesList to those sources.
Apologies if this is super-easy (I hope it is) but I'm a WP8 noob.
I know there are similar threads on SO, but none of them seem to deal with such deep arrays.
I was not stipulating which branch to go down and so it was confused.
System.Diagnostics.Debug.WriteLine(apiData.jobsByDateList[0].jobsList[0].interestLevel);
I was trying to get in using
System.Diagnostics.Debug.WriteLine(apiData.jobsByDateList.jobsList.interestLevel);
and intellisense was freaking out.
With the [0] I'm now able to drill in just fine and don't need the expandoobject. Just needed to sleep on it. :-)

Categories