C# mimic associative array of unknown key-number (like in PHP) - c#

Is there a possibility to create sth. like an associative array like in PHP?
I don't plan to create a game with some player-data, but I could easily explain this way what I want:
player["Name"] = "PName";
player["Custom"]["Gender"] = "Female";
player["Custom"]["Style"] = "S1";
player["Custom"]["Face"]["Main"] = "FM1";
player["Custom"]["Face"]["Eyes"] = "FE1";
player["Custom"]["Height"] = "180";
Also the length has to be dynamic, I don't how many keys there will be:
player["key1"]["key2"]=value
player["key1"]["key2"]["key3"]["key4"]...=value
What I need is sth. I could address like:
string name = player["Name"];
string gender = player["Custom"]["Gender"];
string style = player["Custom"]["Style"];
string faceMain = player["Custom"]["Face"]["Main"];
string faceEyes = player["Custom"]["Face"]["Eyes"];
string height = player["Custom"]["Height"];
Or in some way similar to this.
What I tried till now:
Dictionary<string, Hashtable> player = new Dictionary<string, Hashtable>();
player["custom"] = new Hashtable();
player["custom"]["Gender"] = "Female";
player["custom"]["Style"] = "S1";
But the problem starts here (only works with 2 keys):
player["custom"]["Face"] = new Hashtable();
player["Custom"]["Face"]["Main"] = "FM1";

C# is strongly typed so it seems not easy to replicate this exact behavior.
A "possibility" :
public class UglyThing<K,E>
{
private Dictionary<K, UglyThing<K, E>> dicdic = new Dictionary<K, UglyThing<K, E>>();
public UglyThing<K, E> this[K key]
{
get
{
if (!this.dicdic.ContainsKey(key)) { this.dicdic[key] = new UglyThing<K, E>(); }
return this.dicdic[key];
}
set
{
this.dicdic[key] = value;
}
}
public E Value { get; set; }
}
Usage :
var x = new UglyThing<string, int>();
x["a"].Value = 1;
x["b"].Value = 11;
x["a"]["b"].Value = 2;
x["a"]["b"]["c1"].Value = 3;
x["a"]["b"]["c2"].Value = 4;
System.Diagnostics.Debug.WriteLine(x["a"].Value); // 1
System.Diagnostics.Debug.WriteLine(x["b"].Value); // 11
System.Diagnostics.Debug.WriteLine(x["a"]["b"].Value); // 2
System.Diagnostics.Debug.WriteLine(x["a"]["b"]["c1"].Value); // 3
System.Diagnostics.Debug.WriteLine(x["a"]["b"]["c2"].Value); // 4

Related

C# - How to filter list using enum

I have 2 a classes:
public enum ArticleType
{
News = 1;
SpecialOffer = 2;
Service = 3;
}
public ArticleApiDto
{
public int Id;
public ArticleType Type;
}
Now i have method for getting data from db, something like:
public List<ArticleApiDto> GetAll(List<ArticleType> types)
{
var res = new List<ArticleApiDto>();
res = context.articles.ToList();
//do stuff
}
Now I would like to filter out Articles from res whose type is specified in types from parameter of method.
Problem 1: I actually cant use Contains because ArticleType is enum
Problem 2: List<ArticleType> is because sometimes i want to pass only one type but sometimes two or three. At the moment i cant figure out better better solution.
Can someone help me with this please I was searching for whole noone but cant figure out
To be able to pass more than one type you can do it like:
Make your enum to be Flags (note that values now can't be 1,2,3,4 and should be 1,2,4,8,16,...:
[Flags]
public enum ArticleType
{
News = 1;
SpecialOffer = 2;
Service = 4;
}
Now you add up what you want to filter with |:
filter = ArtickeType.News | ArticleType.Service;
var result = res.Where(x => filter & x.Type != 0).ToList();
or:
filter = ArtickeType.News | ArticleType.Service | ArticleType.SpecialOffer;
var result = res.Where(x => filter.HasFlag(x.Type)).ToList();
To do it with your current enum:
public enum ArticleType
{
News = 1;
SpecialOffer = 2;
Service = 3;
}
var filter = new List<ArticleType> { ArticleType.Service, ArticleType.News};
var result = res.Where(x => filter.Contains(x.Type)).ToList();
This code work with me, I use Linq with contains it may solve your problem.
public static List<ArticleApiDto> GetAll(List<ArticleType> types)
{
var res = new List<ArticleApiDto>();
List<ArticleApiDto> articles = new List<ArticleApiDto>
{
new ArticleApiDto{Id = 1, Type = ArticleType.News },
new ArticleApiDto{Id = 2, Type = ArticleType.SpecialOffer },
};
var newArticales = articles.Where(i =>
{
return types.Contains(i.Type);
}).ToList();
return newArticales;
}

Is there a way to add multiple variables together without implicitly declaration

I have 23 int variables that get assigned value upon form load, is there a shortcut to add them together without implicit addition.
I.E VarAns = Var1 + Var2 + Var3.... + Var 23.
MathsGrp1 = Convert.ToInt32(textBoxMathsGrp1.Text);
MathsGrp3 = Convert.ToInt32(textBoxMathsGrp3.Text);
MathsGrp2 = Convert.ToInt32(textBoxMathsGrp2.Text);
MathsGrp4 = Convert.ToInt32(textBoxMathsGrp4.Text);
EnglishGrp1 = Convert.ToInt32(textBoxEnglishGrp1.Text);
EnglishGrp2 = Convert.ToInt32(textBoxEnglishGrp3.Text);
EnglishGrp3 = Convert.ToInt32(textBoxEnglishGrp2.Text);
EnglishGrp4 = Convert.ToInt32(textBoxEnglishGrp4.Text);
Construction = Convert.ToInt32(textBoxConstruction.Text);
PSD = Convert.ToInt32(textBoxPSD.Text);
Careers = Convert.ToInt32(textBoxCareers.Text);
ASDAN = Convert.ToInt32(textBoxASDAN.Text);
Music = Convert.ToInt32(textBoxMusic.Text);
Spare = Convert.ToInt32(textBoxSpare.Text);
Art = Convert.ToInt32(textBoxArt.Text);
Science = Convert.ToInt32(textBoxScience.Text);
PEGrp1 = Convert.ToInt32(textBoxPEGrp1.Text);
PEGrp2 = Convert.ToInt32(textBoxPEGrp2.Text);
ICT = Convert.ToInt32(textBoxICT.Text);
HairDressing = Convert.ToInt32(textBoxHairDressing.Text);
CookingGrp1 = Convert.ToInt32(textBoxCookingGrp1.Text);
CookingGrp2 = Convert.ToInt32(textBoxCookingGrp2.Text);
CookingGrp3 = Convert.ToInt32(textBoxCookingGrp3.Text);
// int Check = insert Long list of variables here
P.S i know theres a better way to initilise and convert the textbox strings into integers but i want to keep it simple.
public void Function()
{
List<int> Collection = new List<int>();
Collection.Add(1);
Collection.Add(2);
Collection.Add(3);
Collection.Add(7);
Collection.Add(9);
Collection.Add(5);
Collection.Add(25);
foreach (int Elem in Collection)
{
int Result = 0;
Result = Result + Elem;
}
}

cast IEnumerable<T> To Dictionary<int,T>

I have a method with this return type :
IEnumerable<T>
I want to map this Method and fill in a Dictionary.
Expected result is: Dictionary<int, T>
Dictionary<int, LeaveTypeDto> leaveType = new Dictionary<int, LeaveTypeDto>();
LeaveType.GetList(string.Empty).ToDictionary<int, LeaveTypeDto>();
How can I do this?
Note: GetList return type is IEnumerable of LeaveType, and some fields bust be map in LeaveTypeDto and some fields in LeaveType:
"ID,Title,HourlyAvailable,..."
ID is unique and must be use as key in my dictionary, and other field must be map to LeaveTypeDto.
You can specify the key, or the key and value, to take from an enumerable.
If I have a class like so:
public class MyClass
{
public int Id {get;set;}
public string Name {get;set;}
}
I can write the following to get a Dictionary<int, MyClass>:
IEnumerable<MyClass> values;
IDictionary<int, MyClass> valueDict = values.ToDictionary(t => t.Id);
or the following to get a Dictionary<int, string>:
IEnumerable<MyClass> values;
IDictionary<int, string> valueDict = values.ToDictionary(t => t.Id, t => t.Name);
I Defined an Extention Method On LeaveType :
public static DTO.LeaveTypeDto ToDto(this DataModel.Timekeeper.LeaveType leaveType)
{
return new DTO.LeaveTypeDto
{
ID = leaveType.ID,
Code = leaveType.Code,
Title = leaveType.Title,
DailyAvailable = leaveType.DailyAvailable,
HourlyAvailable = leaveType.HourlyAvailable,
ShiftBaseAvailable = leaveType.ShiftBaseAvailable,
PredefinedRemaining = leaveType.PredefinedRemaining,
Active = leaveType.Active,
StandardLeaveType = leaveType.StandardLeaveType,
NotRequestAutomaticaly = leaveType.NotRequestAutomaticaly,
TemplateFileID = leaveType.TemplateFileID,
ObligateChooseSubstituteForDailyLeave = leaveType.ObligateChooseSubstituteForDailyLeave,
ObligateChooseSubstituteForHourlyLeave = leaveType.ObligateChooseSubstituteForHourlyLeave,
ObligateChooseSubstituteForShiftBaseLeave = leaveType.ObligateChooseSubstituteForShiftBaseLeave,
ObligateChooseInsertAttachment = leaveType.ObligateChooseInsertAttachment,
CheckSubstituteLimit = leaveType.CheckSubstituteLimit,
AbsenceRespiteYearXferLimitDays = leaveType.AbsenceRespiteYearXferLimitDays,
AbsenceRespiteYearXferMode = leaveType.AbsenceRespiteYearXferMode,
AbsenceRespiteYearXferValuesSummarize = leaveType.AbsenceRespiteYearXferValuesSummarize,
DailyRequestRegisterRespite = leaveType.DailyRequestRegisterRespite,
DayMaxHourlyMinutes = leaveType.DayMaxHourlyMinutes,
DayWorkMinutes = leaveType.WorkingPeriodMaxHourlyMinutes,
DecreaseAllOnNXOverflow = leaveType.DecreaseAllOnNXOverflow,
DecreaseBasedOnMonthWorkingDays = leaveType.DecreaseBasedOnMonthWorkingDays,
DecreaseDayWorkMinutesOnExtraWorkDays = leaveType.DecreaseDayWorkMinutesOnExtraWorkDays,
DecreaseOnExtraWorkDays = leaveType.DecreaseOnExtraWorkDays,
DecreaseOnHolidays = leaveType.DecreaseOnHolidays,
HourlyInOFFDayAvailable=leaveType.HourlyInOFFDayAvailable,
YearXferValuesSummarize = leaveType.YearXferValuesSummarize,
YearXferPercentage=leaveType.YearXferPercentage,
MaxContinuousDaysLimit = leaveType.MaxContinuousDaysLimit,
HourlyRequestRegisterRespite = leaveType.HourlyRequestRegisterRespite,
YearXferMode = leaveType.YearXferMode,
MonthlyDaysLimit = leaveType.MonthlyDaysLimit,
MonthlyXMinsLimit = leaveType.MonthlyXMinsLimit,
YearXferLimitDays = leaveType.YearXferLimitDays,
YearLimitDays = leaveType.YearLimitDays,
MonthStorable = leaveType.MonthStorable,
YearHourlyMinsLimit = leaveType.YearHourlyMinsLimit,
MonthPreusable = leaveType.MonthPreusable,
MonthlyNXMinsLimit = leaveType.MonthlyNXMinsLimit,
MonthMaxHourlyMinutes = leaveType.MonthMaxHourlyMinutes,
IncreaseAbsenceRespiteRemaining = leaveType.IncreaseAbsenceRespiteRemaining,
DailyLeaveRequestCountPerMonthForOthers = leaveType.DailyLeaveRequestCountPerMonthForOthers,
DailyLeaveRequestDurationPerMonthForOthers = leaveType.DailyLeaveRequestDurationPerMonthForOthers,
ShiftLeaveRequestCountPerMonthForOthers = leaveType.ShiftLeaveRequestCountPerMonthForOthers,
ShiftLeaveRequestDurationPerMonthForOthers = leaveType.ShiftLeaveRequestDurationPerMonthForOthers,
HourlyLeaveRequestCountPerMonthForOthers = leaveType.HourlyLeaveRequestCountPerMonthForOthers,
UseLeaveRequetLimitForOthersPerMonth = leaveType.UseLeaveRequetLimitForOthersPerMonth,
DailyCountPerMonth = leaveType.DailyCountPerMonth,
CheckMeritRemainingInUnpayedRequest = leaveType.CheckMeritRemainingInUnpayedRequest,
DayMinHourlyMinutes = leaveType.DayMinHourlyMinutes,
FirstPresenceRangeHourlyCountPerMonth = leaveType.FirstPresenceRangeHourlyCountPerMonth,
FirstPresenceRangeLenght = leaveType.FirstPresenceRangeLenght,
FirstPresenceRangeMaxHourlyLimit = leaveType.FirstPresenceRangeMaxHourlyLimit,
FirstPresenceRangeMinHourlyLimit= leaveType.FirstPresenceRangeMinHourlyLimit,
HourlyCountPerMonth = leaveType.HourlyCountPerMonth,
HourlyLeaveRequestDurationPerMonthForOthers = leaveType.HourlyLeaveRequestDurationPerMonthForOthers,
IsMonthMaxHourlyStorableToCurrent = leaveType.IsMonthMaxHourlyStorableToCurrent,
LastPresenceRangeHourlyCountPerMonth = leaveType.LastPresenceRangeHourlyCountPerMonth,
LastPresenceRangeLenght = leaveType.LastPresenceRangeLenght,
LastPresenceRangeMaxHourlyLimit = leaveType.LastPresenceRangeMaxHourlyLimit,
LastPresenceRangeMinHourlyLimit = leaveType.LastPresenceRangeMinHourlyLimit,
MiddlePresenceRangeHourlyCountPerMonth = leaveType.MiddlePresenceRangeHourlyCountPerMonth,
MiddlePresenceRangeMaxHourlyLimit = leaveType.MiddlePresenceRangeMaxHourlyLimit,
MiddlePresenceRangeMinHourlyLimit = leaveType.MiddlePresenceRangeMinHourlyLimit,
MonthMaxDailyDays=leaveType.MonthMaxDailyDays,
MonthMaxHourlyStorable=leaveType.MonthMaxHourlyStorable,
ShiftBaseCountPerMonth=leaveType.ShiftBaseCountPerMonth,
SuspendsPersonnelStatus=leaveType.SuspendsPersonnelStatus,
WorkingPeriodMaxHourlyMinutes=leaveType.WorkingPeriodMaxHourlyMinutes,
YearXMonthMaxHourlyferLimitHours=leaveType.YearXMonthMaxHourlyferLimitHours,
YearXMonthMaxHourlyferMode=leaveType.YearXMonthMaxHourlyferMode
};
}
and by this Line My problem Solved.:)
GetList(string.Empty).ToDictionary(lt => lt.ID, lt => lt.ToDto())
If ID is part of LeaveTypeDto
var q = GetList(string.Empty).ToDictionary(b => b.ID);
else use the select overload to get index and then use ToDictionary()
var q = GetList(string.Empty).Select((element, index) => new { i= index, e = element }).ToDictionary(b => b.i, b => b.e);
What about System.Linq
GetList(string.Empty).ToDictionary(k => int.Parse(k.ID))
Note, the previous example is valid if ID is string, if it is int then even simpler
GetList(string.Empty).ToDictionary(k => k.ID)

c# use one variable value to set a second from a fixed list

I'm parsing a CSV file in a c# .net windows form app, taking each line into a class I've created, however I only need access to some of the columns AND the files being taken in are not standardized. That is to say, number of fields present could be different and the columns could appear in any column.
CSV Example 1:
Position, LOCATION, TAG, NAME, STANDARD, EFFICIENCY, IN USE,,
1, AFT-D3, P-D3101A, EQUIPMENT 1, A, 3, TRUE
2, AFT-D3, P-D3103A, EQUIPMENT 2, B, 3, FALSE
3, AFT-D3, P-D2301A, EQUIPMENT 3, A, 3, TRUE
...
CSV Example 2:
Position, TAG, STANDARD, NAME, EFFICIENCY, LOCATION, BACKUP, TESTED,,
1, P-D3101A, A, EQUIPMENT 1, 3, AFT-D3, FALSE, TRUE
2, P-D3103A, A, EQUIPMENT 2, 3, AFT-D3, TRUE, FALSE
3, P-D2301A, A, EQUIPMENT 3, 3, AFT-D3, FALSE, TRUE
...
As you can see, I will never know the format of the file I have to analyse, the only thing I know for sure is that it will always contain the few columns that I need.
My solution to this was to ask the user to enter the columns required and set as strings, the using their entry convert that to a corresponding integer that i could then use as a location.
string standardInpt = "";
string nameInpt = "";
string efficiencyInpt = "";
user would then enter a value from A to ZZ.
int standardLocation = 0;
int nameLocation = 0;
int efficiencyLocation = 0;
when the form is submitted. the ints get their final value by running through an if else... statement:
if(standard == "A")
{
standardLocation = 0;
}
else if(standard == "B")
{
standardLocation = 1;
}
...
etc running all the way to if VAR1 == ZZ and then the code is repeated for VAR2 and for VAR3 etc..
My class would partially look like:
class Equipment
{
public string Standard { get; set;}
public string Name { get; set; }
public int Efficiency { get; set; }
static Equipment FromLine(string line)
{
var data = line.split(',');
return new Equipment()
{
Standard = data[standardLocation],
Name = [nameLocation],
Efficiency = int.Parse(data[efficiencyLocation]),
};
}
}
I've got more code in there but i think this highlights where I would use the variables to set the indexes.
I'm very new to this and I'm hoping there has got to be a significantly better way to achieve this without having to write so much potentially excessive, repetitive If Else logic. I'm thinking some kind of lookup table maybe, but i cant figure out how to implement this, any pointers on where i could look?
You could make it automatic by finding the indexes of the columns in the header, and then use them to read the values from the correct place from the rest of the lines:
class EquipmentParser {
public IList<Equipment> Parse(string[] input) {
var result = new List<Equipment>();
var header = input[0].Split(',').Select(t => t.Trim().ToLower()).ToList();
var standardPosition = GetIndexOf(header, "std", "standard", "st");
var namePosition = GetIndexOf(header, "name", "nm");
var efficiencyPosition = GetIndexOf(header, "efficiency", "eff");
foreach (var s in input.Skip(1)) {
var line = s.Split(',');
result.Add(new Equipment {
Standard = line[standardPosition].Trim(),
Name = line[namePosition].Trim(),
Efficiency = int.Parse(line[efficiencyPosition])
});
}
return result;
}
private int GetIndexOf(IList<string> input, params string[] needles) {
return Array.FindIndex(input.ToArray(), needles.Contains);
}
}
You can use the reflection and attribute.
Write your samples in ,separated into DisplayName Attribute.
First call GetIndexes with the csv header string as parameter to get the mapping dictionary of class properties and csv fields.
Then call FromLine with each line and the mapping dictionary you just got.
class Equipment
{
[DisplayName("STND, STANDARD, ST")]
public string Standard { get; set; }
[DisplayName("NAME")]
public string Name { get; set; }
[DisplayName("EFFICIENCY, EFFI")]
public int Efficiency { get; set; }
// You can add any other property
public static Equipment FromLine(string line, Dictionary<PropertyInfo, int> map)
{
var data = line.Split(',').Select(t => t.Trim()).ToArray();
var ret = new Equipment();
Type type = typeof(Equipment);
foreach (PropertyInfo property in type.GetProperties())
{
int index = map[property];
property.SetValue(ret, Convert.ChangeType(data[index],
property.PropertyType));
}
return ret;
}
public static Dictionary<PropertyInfo, int> GetIndexes(string headers)
{
var headerArray = headers.Split(',').Select(t => t.Trim()).ToArray();
Type type = typeof(Equipment);
var ret = new Dictionary<PropertyInfo, int>();
foreach (PropertyInfo property in type.GetProperties())
{
var fieldNames = property.GetCustomAttribute<DisplayNameAttribute>()
.DisplayName.Split(',').Select(t => t.Trim()).ToArray();
for (int i = 0; i < headerArray.Length; ++i)
{
if (!fieldNames.Contains(headerArray[i])) continue;
ret[property] = i;
break;
}
}
return ret;
}
}
try this if helpful:
public int GetIndex(string input)
{
input = input.ToUpper();
char low = input[input.Length - 1];
char? high = input.Length == 2 ? input[0] : (char?)null;
int indexLow = low - 'A';
int? indexHigh = high.HasValue ? high.Value - 'A' : (int?)null;
return (indexHigh.HasValue ? (indexHigh.Value + 1) * 26 : 0) + indexLow;
}
You can use ASCII code for that , so no need to add if else every time
ex.
byte[] ASCIIValues = Encoding.ASCII.GetBytes(standard);
standardLocation = ASCIIValues[0]-65;

how to add new object for each item in the list

I have assigned data to a list like below.
foreach (var items in niledetails)
{
cruiseDetails.Add(new CruiseDetails()
{
mainID = items.cruiseID,
idL = items.idlength,
mainimageUrl = items.cruiseimageUrl,
adultPrice = items.adultprice,
location = items.cruiseLocation,
numberofDays = items.numberofdays,
description = items.description,
embarkationP = items.embarkationport,
cruiseTyp = items.cruisetype,
childPrice = items.childprice,
totalPrice = items.totalprice,
farecode = items.referenceNumber,
experience = items.cruiseName,
fullcategoryname = items.cruiseCabname
});
}
this works fine.this has 30 objects and for each object has 14 key/values.now what I want is to add additional value for each object in the list.
that means something like this
for(int i=0;i<cruiseDetails.Count();i++)
{
//for each object I want to add another key value( 15th) for all 30 objects.
}
How can I do that. hope your help.
NOTE : I'm not looking for this. EX:
cruiseDetails.Insert(0, new someModel() { city = "All"});
for(int i=0;i<cruiseDetails.Count();i++)
{
cruiseDetails[i].additionalKey = val;
}
try this code
Try this:
foreach (CruiseDetails myCruiseDetail in cruiseDetails){
myCruiseDetail.myNewKey = myNewValue;
}
try something as this:
List<Tuple<int, CruiseDetails>> list = new List<Tuple<int, CruiseDetails>>();
CruiseDetails> sourceCruises = new List<CruiseDetails>();
int i = 0;
foreach (var c in sourceCruises)
{
list.Add(new Tuple<int, CruiseDetails>(i, c));
i++;
}
Set your properties in your loop
for(int i=0;i<cruiseDetails.Count();i++)
{
cruiseDetails[i].mynewproperty = somevalue;
}

Categories