How do I turn this:
string x = "key:value|key:value|key:value|key:value";
into this?
List<myClass> listObj;
myClass definition:
public class myClass
{
public string keyName { get; set; }
public string keyValue { get; set; }
}
There has to be a way to do it using LINQ or something :)
thanks in advance!
* NOTE *
I should add I know how to do this splitting it and looping through it, but there has to be a better way :)
This will require separate ToList() call, but I like query syntax for its declarative nature:
from s in x.Split('|')
let parts = s.Split(':')
select new myClass {
keyName = parts[0],
keyValue = parts[1]
}
Or you can use fluent syntax:
x.Split('|')
.Select(s => {
var parts = s.Split(':');
return new myClass {
keyName = parts[0],
keyValue = parts[1]
};
}).ToList()
Well, since you really wanted to avoid splitting and looping...
public List<MyClass> Parse(string base, string workingName, string workingValue,
bool processingName = true,
List<MyClass> workingList = null, int index = 0)
{
if (workingList == null)
workingList = new List<MyClass>();
if (index >= base.Length)
{
return workingList;
}
if (base[index] = '|')
{
workingList.Add(new MyClass { keyName = workingName, keyValue = workingValue });
return Parse(base, "", "", true, workingList, index + 1);
}
else if (base[index] = ':')
{
return Parse(base, workingName, "", false, workingList, index + 1);
}
else if (processingName)
{
return Parse(base, workingName + base[index], "", processingName, workingList, index + 1);
}
else
{
return Parse(base, workingName, workingValue + base[index], processingName, workingList, index + 1);
}
}
But please, for the love of whatever you hold dear, don't do anything even remotely resembling that (and yes, this is untested, hand-written code, so there are probably errors - just making a joke about avoiding things).
Do this if you prefer to use your custom class instead of Dictionary
var result = from y in x.Split('|')
let obj = y.Split(':')
select new myClass{keyName = obj[0], keyValue = obj[1]};
var list = result.ToList();
Related
is it possible in Linq to select from IEnumerable of this object
public class Foo
{
public int Id { get; set; }
public string Type { get; set; }
}
where Type is "" ?
if I loop over the list with that
foreach (Foo f in dataFoos)
{
Console.WriteLine(f.Id + f.Type);
}
it looks like
1one
2
3three
I have tried
var emptyType0 = dataFoos.Where(f => f.Type.Length <= 1);
var emptyType1 = dataFoos.Where(f => f.Type == null || f.Type == "");
both did not return any result. Any hint on how to properly check if String values are empty ?
if I do that
var df = dataFoos.Where(f => String.IsNullOrWhiteSpace(f.Type));
foreach (Foo f in df)
{
Console.WriteLine(f.Id + f.Type);
}
var df1 = dataFoos.Where(f => !String.IsNullOrWhiteSpace(f.Type));
foreach (Foo f in df1)
{
Console.WriteLine(f.Id + f.Type);
}
the second loop does not return any value
I am using dotnetcore c#. Thanks for any hint
This should cover almost every type of null/blank/just whitespace
var emptyType1 = foos.Where(f => String.IsNullOrWhiteSpace(f.Type));
but more likely what you want to do is exclude those - not include them
var dataFoos = foos.Where(f => !String.IsNullOrWhiteSpace(f.Type));
foreach (Foo f in dataFoos)
{
Console.WriteLine(f.Id + f.Type);
}
String Array 1: (In this format: <MENU>|<Not Served?>|<Alternate item served>)
Burger|True|Sandwich
Pizza|True|Hot Dog
String Array 2: (Contains Menu)
Burger
Pizza
Grill Chicken
Pasta
I need the menu is served or any alternate item served for that particular item.
Code:
for(int i = 0; i < strArr2.Length; i++)
{
if(strArr2.Any(_r => _r.Split('|').Any(_rS => _rS.Contains(strArr1[i]))))
{
var menu = strArr2[i];
var alternate = ? // need to get alternate item
}
}
As I commented in the code, how to get the alternate item in that string array? Please help, thanks in advance.
P.S: Any help to trim if condition is also gladly welcome.
Instead of any, you may use Where to get the value matching.
#Markus is having the detailed answer, I am just using your code to find a quick fix for you.
for(int i = 0; i < strArr2.Length; i++)
{
if(strArr2.Any(_r => _r.Split('|').Any(_rS => _rS.Contains(strArr1[i]))))
{
var menu = strArr2[i];
var alternate = strArr2.Where(_rs => _rs.Split('|').Any(_rS => _rS.Contains(strArr1[i]))).First().Split('|').Last();
}
}
In order to simplify your code, it is a good idea to better separate the tasks. For instance, it will be much easier to handle the contents of string array 1 after you have converted the contents into objects, e.g.
class NotServedMenu
{
public string Menu { get; set; }
public bool NotServed { get; set; }
public string AlternateMenu { get; set; }
}
Instead of having an array of strings, you can read the strings to a list first:
private IEnumerable<NotServedMenu> NotServedMenusFromStrings(IEnumerable<string> strings)
{
return (from x in strings select ParseNotServedMenuFromString(x)).ToArray();
}
private NotServedMenu ParseNotServedMenuFromString(string str)
{
var parts = str.Split('|');
// Validate
if (parts.Length != 3)
throw new ArgumentException(string.Format("Unable to parse \"{0}\" to an object of type {1}", str, typeof(NotServedMenu).FullName));
bool notServedVal;
if (!bool.TryParse(parts[1], out notServedVal))
throw new ArgumentException(string.Format("Unable to read bool value from \"{0}\" in string \"{1}\".", parts[1], str));
// Create object
return new NotServedMenu() { Menu = parts[0],
NotServed = notServedVal,
AlternateMenu = parts[2] };
}
Once you can use the objects, the subsequent code will be much cleaner to read:
var notServedMenusStr = new[]
{
"Burger|True|Sandwich",
"Pizza|True|Hot Dog"
};
var notServedMenus = NotServedMenusFromStrings(notServedMenusStr);
var menus = new[]
{
"Burger",
"Pizza",
"Grill Chicken",
"Pasta"
};
var alternateMenus = (from m in menus join n in notServedMenus on m equals n.Menu select n);
foreach(var m in alternateMenus)
Console.WriteLine("{0}, {1}, {2}", m.Menu, m.NotServed, m.AlternateMenu);
In this sample, I've used a Linq join to find the matching items.
You could do something like that
string[] strArr1 = { "Burger|True|Sandwich", "Pizza|True|Hot Dog" };
string[] strArr2 = { "Burger", "Pizza", "Grill Chicken", "Pasta" };
foreach (string str2 in strArr2)
{
string str1 = strArr1.FirstOrDefault(str => str.Contains(str2));
if (str1 != null)
{
string[] splited = str1.Split('|');
string first = splited[0];
bool condition = Convert.ToBoolean(splited[1]);
string second = splited[2];
}
}
is there a way to combine these to list items into one list item ? i am sorry if this is a begginer mistake
List<string> values = new List<string>();
foreach (Feature f in allFeatures)
{
if (f.ColumnValues.ContainsKey(layercode)&& f.ColumnValues.ContainsKey(layercode2))
{
if (!values.Contains(f.ColumnValues[layercode].ToString()) && !values.Contains(f.ColumnValues[layercode2].ToString()))
{
values.Add(f.ColumnValues[layercode].ToString());
values.Add(f.ColumnValues[layercode2].ToString());
}
}
}
You can use a List of Tuples, a Dictionary, or create a class. I will not go into depth explaining these as you should be able to easily search and find other questions all about these. Some of this is from memory so syntax might be a bit off.
List of Tuples
List<Tuple<string,string>> values = new List<Tuple<string,string>>();
//...
if ( !values.Any(v=>v.Item1 == f.ColumnValues[layercode].ToString()) && !values.Any(v=>v.Item2 == f.ColumnValues[layercode2].ToString()) )
{
values.Add( Tuple.Create(f.ColumnValues[layercode].ToString(),
f.ColumnValues[layercode2].ToString()) );
}
Dictionary
Dictionary<string,string> values = new Dictionary<string,string> ();
//...
if (!values.ContainsKey(f.ColumnValues[layercode].ToString()) && !values.ContainsValue(f.ColumnValues[layercode2].ToString()))
{
values[f.ColumnValues[layercode].ToString()] = f.ColumnValues[layercode2].ToString();
}
List of class instances
public class LayerCodePair {
public string Code1 {get;set;}
public string Code2 {get;set;}
} // declared outside of method
...
List<LayerCodePair> values = new List<LayerCodePair>();
//...
if (!values.Any(v=> v.Code1 == f.ColumnValues[layercode].ToString()) && !values.Any(v=>v.Code2 == f.ColumnValues[layercode2].ToString()))
{
values.Add(new LayerCodePair{
Code1 = f.ColumnValues[layercode].ToString(),
Code2 = f.ColumnValues[layercode2].ToString()
});
}
It should work for you, using ";" character as a separator:
List<string> values = new List<string>();
foreach (Feature f in allFeatures)
{
var columnValues = f.ColumnValues;
var firstLayerCode = columnValues[layercode].ToString();
var secondLayerCode = columnValues[layercode2].ToString();
if (columnValues.ContainsKey(layercode) && columnValues.ContainsKey(layercode2))
{
if (!values.Contains(firstLayerCode) && !values.Contains(secondLayerCode))
{
var combinedValue = firstLayerCode + ";" + secondLayerCode;
values.Add(combinedValue);
}
}
}
So I have a .csv file with 2 columns looking a bit like this:
01,23
02,45
03,178
etc.
Now I want to read the csv file, give it a value for the first column, and get the corresponding value from the second column back. (so say I give it a value of 03, it should give me 178 back)
Here's the code I've got so far but what should go in the if statement?
public int CalculateNextLevel(int current_xp, int current_lvl)
{
var reader = new StreamReader(File.OpenRead(#"C:\Users\Lennart\Desktop\Legends of Raymere\Files\Lvl.csv"));
List<int> levels = new List<int>();
List<int> exp = new List<int>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
levels.Add(Convert.ToInt32(values[0]));
exp.Add(Convert.ToInt32(values[1]));
foreach (int level in levels)
{
if (current_lvl == level)
{
}
}
}
return XP_to_nxt_lvl;
}
You can use a Dictionary instead
var expValues = new Dictionary<int, int>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
expValues.Add(Convert.ToInt32(values[0]), Convert.ToInt32(values[1]));
}
// Retrieve value based on level
if (expValues.ContainsKey(3))
{
int level03Exp = expValues[3];
}
try this
static void Main(string[] args)
{
string key = "03";
GetValue(key);
}
private static int GetValue(string key)
{
var lines = File.ReadAllLines("test.txt");
var dictonary = lines.ToDictionary(dict =>
{
return dict.Split(',')[0];
});
int valInt = int.Parse(dictonary[key].Split(',')[1]);
return valInt;
}
You should use Linq like this :
foreach (int level in levels)
{
if (current_lvl == level)
{
XP_to_nxt_lvl = exp[levels.IndexOf(level)];
}
}
You could use LINQ and a Lookup<TKey, TValue> which allows duplicate keys but is similar to a Dictionary. If the key(level) is not present you get an empty collection of xp's:
private ILookup<int, int> LevelLookup = null;
public void LoadAllLevels()
{
LevelLookup = File.ReadLines(#"C:\Temp\Lvl.csv")
.Select(l => l.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
.Select(cols =>
{
int level = 0, xp = 0;
bool validLine = cols.Length == 2;
if(validLine)
validLine = int.TryParse(cols[0].Trim(), out level);
if(validLine)
validLine = int.TryParse(cols[1].Trim(), out xp);
return new{ level, xp, validLine };
})
.Where(x => x.validLine)
.ToLookup(x => x.level, x => x.xp);
}
public int? CalculateNextLevel(int current_xp, int current_lvl)
{
int? nextLevel = null;
var xps = LevelLookup[current_lvl];
if (xps.Any())
nextLevel = xps.First();
return nextLevel;
}
You only need to initialize it once or when the file changed via LoadAllLevels().
For example:
LoadAllLevels();
int level3 = 3;
int level4 = 4;
int? xp3 = CalculateNextLevel(100,level3);
int? xp4 = CalculateNextLevel(150,level4);
I have used nullables to differentiate between XP=0 and a level has yet no defined xp.
bool hasLev4XP = xp4.HasValue;
if(hasLev4XP)
{
int lev4XpNeeded = xp4.Value;
}
If the level is guaranteed to be unique you could also use ToDictionary to create a dictionary and use similar code as above.
Ok let me explain clearly what i want to achieve
It will be an object which will contain the below data - like an sql server table
BigInt parameter1
BigInt parameter2
string parameter3
these parameter1 and parameter2 both will compose the index (like primary key in sql-server table)
So this object will have like 500000 records like the above
And i will make fast look ups from this object like
return parameter3 where parameter1 <= value and value <= parameter2
What can be used for this ?
So far i tried these and they are slow
DataView.RowFilter = super slow
static Dictionary<Int64, KeyValuePair<Int64, string>> = slower than database query
Database query = where parameter1 & parameter2 composes primary key = slow since i need to make over 500000 query.
I also searched many questions at stackoverflow and none of them targeting between operator at integer keys. They are all multiple string key.
C# 4.0
Quick and dirty sketch:
public class GeoIp
{
private class GeoIpRecord
{
public long StartIp;
public long EndIp;
public string Iso;
}
private class GeoIpRecordComparer: IComparer<GeoIpRecord>
{
public int Compare(GeoIpRecord x, GeoIpRecord y)
{
return x.StartIp.CompareTo(y.StartIp);
}
}
private List<GeoIpRecord> geoIp;
private IComparer<GeoIpRecord> comparer;
public GeoIp()
{
this.geoIp = new List<GeoIpRecord>(500000)
{
new GeoIpRecord { StartIp = 1, EndIp = 2, Iso = "One" },
new GeoIpRecord { StartIp = 3, EndIp = 5, Iso = "Three" },
new GeoIpRecord { StartIp = 6, EndIp = 6, Iso = "Six" },
new GeoIpRecord { StartIp = 7, EndIp = 10, Iso = "Seven" },
new GeoIpRecord { StartIp = 15, EndIp = 16, Iso = "Fifteen" },
};
this.comparer = new GeoIpRecordComparer();
}
public string GetIso(long ipValue)
{
int index = this.geoIp.BinarySearch(new GeoIpRecord() { StartIp = ipValue }, this.comparer);
if (index < 0)
{
index = ~index - 1;
if (index < 0)
{
return string.Empty;
}
}
GeoIpRecord record = this.geoIp[index];
if (record.EndIp >= ipValue)
{
return record.Iso;
}
else
{
return string.Empty;
}
}
}
And the code that confirms the solution:
GeoIp geoIp = new GeoIp();
var iso1 = geoIp.GetIso(1); // One
var iso2 = geoIp.GetIso(2); // One
var iso3 = geoIp.GetIso(3); // Three
var iso4 = geoIp.GetIso(4); // Three
var iso5 = geoIp.GetIso(5); // Three
var iso6 = geoIp.GetIso(6); // Six
var iso7 = geoIp.GetIso(7); // Seven
var iso11 = geoIp.GetIso(11); //
var iso15 = geoIp.GetIso(15); // Fifteen
var iso17 = geoIp.GetIso(17); //
The List has to be filled with an ordered data.
List.BinarySearch Method (T, IComparer)
I don't think [that] ranges overlap.
This simplifies the problem a great deal: rather than performing a two-dimensional search, you can sort your list, and perform a one-dimensional binary search, like this:
var data = new List<Tuple<long,long,string>>(TotalCount);
var cmp = new TupleComparer();
data.Sort(cmp);
long item = ... // The item to be searched
var pos = data.BinarySearch(Tuple.Create(item, long.MinValue, String.Empty), cmp);
// It appears that your data has only non-empty strings, so it is guaranteed that
// pos is going to be negative, because Item3, the last tie-breaker, will be smaller
// than anything that you may have in the table
pos = ~pos;
if (pos != data.Count && data[pos].Item1 <= item && data[pos].Item2 >= item) {
Console.WriteLine("Found: '{0}'", data[pos].Item3);
} else {
Console.WriteLine("Not found");
}
Here is the TupleComparer class:
class TupleComparer : IComparer<Tuple<long,long,string>> {
public int Compare(Tuple<long,long,string> x, Tuple<long,long,string> y) {
var res = x.Item1.CompareTo(y.Item1);
if (res != 0) return res;
res = x.Item2.CompareTo(y.Item2);
return (res != 0) ? res : String.CompareOrdinal(x.Item3, y.Item3);
}
}