How to correctly divide List<string>? - c#

I have List<string> {"", "1,5,4", "h", "5,8", "1"}. I need to divide into 3 List<int>. This is my code:
var parseString = condition.Trim().Split(separator).ToList();
var numberSections = new List<string>();
var numberRow = new List<string>();
var numberCell = new List<string>();
foreach (var str in parseString) {
if (int.TryParse(str.Substring(0, 1), out i) && numberSections.Count == 0) {
numberSections.Add(str);
parseString.Remove(str);
}
if (int.TryParse(str.Substring(0, 1), out i) && numberRow.Count == 0) {
numberRow.Add(str);
parseString.Remove(str);
}
if (int.TryParse(str.Substring(0, 1), out i) && numberCell.Count == 0) {
numberCell.Add(str);
parseString.Remove(str);
}
}
But it do not working. How I can do it?

Here is a LINQ version for it
var result = list.Select(x => x.Split(",".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries)) // now we have List<List<string>>
.Select(x => x.Select(y =>
{
int value;
var isInt = int.TryParse(y, out value);
return isInt ? value : (int?)null;
})) // convert each element of inner list to null or its int values
// we have a List<List<int?>>
.Where(x => x.Any() && x.All(y => y.HasValue)) // only select lists which contains only integers
.ToList();

Related

C# LINQ to create a temporary variable with dynamic index

var newList = data.GroupBy(x => new { x.Symbol })
.Select
(
x =>
{
var subList = x.OrderBy(y => y.Date).ToList();
return subList.Select((y, idx) => new
{
Symbol = y.Symbol,
Close = y.Close,
Date = y.Date,
/*p = (idx < 1) ? null : subList.Skip(idx - 1)
.Take(2).Select(o => o.Close).ToList()*/,
Vol = (idx < 1) ? 0 : new DescriptiveStatistics
(subList.Skip(idx - 1).Take(2).Select(o => (double)o.Close/(double)
subList.ElementAt(idx - 1).Close).ToList()).StandardDeviation,
});
}
)
.SelectMany(x => x)
.ToList();
I want create a variable p = (idx < 1) ? null : subList.Skip(idx - 1).Take(2).Select(o => o.Close).ToList() with the same index idx of Vol but do not appear in the return just a temporary variable(but have to use the synchronous idx as Vol). So where to write down this p or how to change the syntax
You can hold a temp variable, just like you have done with subList although I dont have a test structure to build against something like this below should work.
var newList = data.GroupBy(x => new { x.Symbol })
.Select
(
x =>
{
var subList = x.OrderBy(y => y.Date).ToList();
return subList.Select((y, idx) => { //return is a function not an object
var p = (idx < 1) ? null : subList.Skip(idx - 1).Take(2).Select(o => o.Close).ToList(); //your p
return new //this return returns the object definition
{
Symbol = y.Symbol,
Close = y.Close,
Date = y.Date,
Vol = p == null ? 0 : new DescriptiveStatistics(subList.Skip(idx - 1).Take(2).Select(o => (double)o.Close / (double)subList.ElementAt(idx - 1).Close).ToList()).StandardDeviation,
};
});
}
)
.SelectMany(x => x)
.ToList();

C# LINQ to merge two breakup

I am looking for simple LINQ to solve this:
string[] breakups = new[]
{
"YQ:50/BF:50/YR:50",
"YQ:50/SR:50",
"YQ:50/BF:50/YR:50",
"XX:00 .... and so on"
};
// LINQ expression
string expectedResult = "YQ:150/BF:100/YR:100/SR:50";
My alternate solution as follows
public static string CalcRTBreakup(string pstrBreakup)
{
string lstrBreakup = pstrBreakup;
try
{
string[] lstrArrRB = lstrBreakup.Split('#');
string[] lstrArrBreakupSplit = new string[lstrBreakup.Split('#')[0].Split('|').Length];
for (int count = 0; count < lstrArrRB.Length; count++)
{
string[] lstrArrtemp = lstrArrRB[count].Split('|');
for (int countinner = 0; countinner < lstrArrtemp.Length; countinner++)
{
if (string.IsNullOrEmpty(lstrArrBreakupSplit[countinner]))
{
if (string.IsNullOrEmpty(lstrArrtemp[countinner]))
continue;
lstrArrBreakupSplit[countinner] = lstrArrtemp[countinner];
}
else
{
if (string.IsNullOrEmpty(lstrArrtemp[countinner]))
continue;
lstrArrBreakupSplit[countinner] += "/" + lstrArrtemp[countinner];
}
}
}
for (int count = 0; count < lstrArrBreakupSplit.Length; count++)
{
if (string.IsNullOrEmpty(lstrArrBreakupSplit[count]))
continue;
lstrArrBreakupSplit[count] = CalcRTBreakupDict(lstrArrBreakupSplit[count].TrimEnd('/')).TrimEnd('/');
}
lstrBreakup = string.Empty;
foreach (string strtemp in lstrArrBreakupSplit)
{
lstrBreakup += strtemp + '|';
}
return lstrBreakup;
}
catch (Exception)
{
return "";
}
}
public static string CalcRTBreakupDict(string pstrBreakup)
{
string lstrBreakup = pstrBreakup;
Dictionary<string, double> ldictDreakup = new Dictionary<string, double>();
try
{
lstrBreakup = lstrBreakup.TrimEnd('/').Trim();
string[] lstrArrBreakup = lstrBreakup.Split('/');
foreach (string strBr in lstrArrBreakup)
{
string[] lstrBreakupCode = strBr.Split(':');
if (!ldictDreakup.Keys.Contains(lstrBreakupCode[0]))
{
double lintTemp = 0; double.TryParse(lstrBreakupCode[1], out lintTemp);
ldictDreakup.Add(lstrBreakupCode[0], lintTemp);
}
else
{
double lintTemp = 0; double.TryParse(lstrBreakupCode[1], out lintTemp);
lintTemp = lintTemp + ldictDreakup[lstrBreakupCode[0]];
ldictDreakup.Remove(lstrBreakupCode[0]);
ldictDreakup.Add(lstrBreakupCode[0], lintTemp);
}
}
lstrBreakup = string.Empty;
foreach (string dictKey in ldictDreakup.Keys)
{
lstrBreakup += dictKey + ":" + ldictDreakup[dictKey] + "/";
}
return lstrBreakup;
}
catch (Exception)
{
return pstrBreakup;
}
}
string[] breakups =
{
"YQ:50/BF:50/YR:50",
"YQ:50/SR:50",
"YQ:50/BF:50/YR:50",
"XX:00"
};
var groups = from line in breakups // these are our items in the array
from item in line.Split('/') // each one will be split up at '/'
let pair = item.Split(':') // each pair is split at ':'
let key = pair[0] // our key is the first item...
let value = int.Parse(pair[1]) // and the value is the second
group value by key // let's group by key
into singleGroup
let sum = singleGroup.Sum() // and build each group's sum
where sum > 0 // filter out everything <= 0
select singleGroup.Key + ":" + sum; // and build the string
var result = string.Join("/", groups);
If you don't need an ordering by value, you can simply do
var res = string.Join("/", breakups
.SelectMany(m => m.Split('/'))
.Select(x => x.Split(':'))
.GroupBy(m => m[0])
.Select(m => string.Format("{0}:{1}", m.Key, m.Sum(g => Int32.Parse(g[1])))));
if you need ordering
var res = string.Join("/", breakups
.SelectMany(m => m.Split('/'))
.Select(x => x.Split(':'))
.GroupBy(m => m[0])
.Select(m => new
{
key = m.Key,
val = m.Sum(g => Int32.Parse(g[1]))
})
.OrderByDescending(m => m.val)
.Select(m => string.Format("{0}:{1}", m.key, m.val)));
var sums = breakups.SelectMany(breakup => breakup.Split('/'))
.Select(s => new { Code = s.Substring(0, 2), Value = int.Parse(s.Substring(2)) })
.GroupBy(pair => pair.Code)
.Select(group => string.Format("{0}/{1}", group.Key, group.Sum(x => x.Value)));
string result = string.Join("/", sums);
The code may contain syntax errors, cause I've not tested it.
Looking at the result I am assuming you add the values after the semicolon where the label before the semicolon match.
It can be treated as a little fun quiz, maybe more suited to another stackexchange site, but anyway.
This can be achieved with a simple (but not very short) linq expression:
breakups
.Select(c => c.Split('/'))
.SelectMany(c => c)
.Select(c => new
{
Label = c.Split(':')[0],
Value = Convert.ToInt32(c.Split(':')[1])
})
.GroupBy(c => c.Label)
.Select(c => new
{
Label = c.Key,
Value = c.Sum(x => x.Value)
})
.OrderByDescending(c => c.Value)
.Select(c => c.Label + ":" + c.Value)
.Aggregate((s1,s2) => s1 + "/" + s2)

Increment in LINQ Query

Hello I have the following code which returns me List of type Listitem. I want to increase the value by one for every ListItem Selected.
public static List<System.Web.UI.WebControls.ListItem> GetMyCompassTUTListContent(List<int> ContentID, Int32 CountryID)
{
int Counter = 0;
List<System.Web.UI.WebControls.ListItem> litems = new List<System.Web.UI.WebControls.ListItem>();
using (DbDataContext objContext = new DbDataContext())
{
if (CountryID == (int)MyCompassBLL.Constants.Country.Australia)
{
litems = objContext.Contents.Where(x => ContentID.Contains(x.ID)).Select(x => new System.Web.UI.WebControls.ListItem { Text = x.Text, y = (y + 1) }).ToList();
}
else
{
litems = objContext.ContentCountries.Where(x => ContentID.Contains(x.ContentID) && x.CountryID == CountryID).Select(x => new System.Web.UI.WebControls.ListItem { Text = x.Text, Value = (Counter + 1).ToString() }).ToList();
}
}
return litems;
}
Please help me in this. I am not able to that since I am not able to find the way of how to reassign value to variable counter after increment.
Thanks in advance
There is an overload of Select that also provides the index of the current item. You can use that. However, most DB query providers don't support it, so you'll need to do your DB query, then ensure that the Enumerable overload of Select is called to add the index:
public static List<ListItem> GetMyCompassTUTListContent(
List<int> ContentID, Int32 CountryID)
{
//Note this is IEnumerable, not IQueryable, this is important.
IEnumerable<string> query;
using (DbDataContext objContext = new DbDataContext())
{
if (CountryID == (int)MyCompassBLL.Constants.Country.Australia)
{
query = objContext.Contents.Where(x => ContentID.Contains(x.ID))
.Select(x => x.Text);
}
else
{
query = objContext.ContentCountries
.Where(x => ContentID.Contains(x.ContentID)
&& x.CountryID == CountryID)
.Select(x => x.Text);
}
return query.Select((text, index) => new ListItem
{
Text = text,
Value = (index + 1).ToString(),
})
.ToList();
}
}
Use ++Counter instead of ( Counter + 1)
[UPDATE]
try to increment your Counter before put it into the Select():
else
{
Counter++;
litems = objContext.ContentCountries.Where(x => ContentID.Contains(x.ContentID) && x.CountryID == CountryID).Select(x => new System.Web.UI.WebControls.ListItem { Text = x.Text, Value = Counter.ToString() }).ToList();
}

Linq Conditional .Any() Select

How can I perform a conditional select on a column value, where I have a preference over which value is returned. If I can't find the top choice, I settle on the next, if available, and then if not the next, etc. As it looks right now, it would take 3 total queries. Is there a way to simplify this further?
var myResult = string.Empty;
if (myTable.Where(x => x.ColumnValue == "Three").Any())
{
myResult = "Three"; // Can also be some list.First().Select(x => x.ColumnValue) if that makes it easier;
}
else if (myTable.Where(x => x.ColumnValue == "One").Any())
{
myResult = "One";
}
else if (myTable.Where(x => x.ColumnValue == "Two").Any())
{
myResult = "Two";
}
else
{
myResult = "Four";
}
You could use a string[] for your preferences:
string[] prefs = new[]{ "One", "Two", "Three" };
string myResult = prefs.FirstOrDefault(p => myTable.Any(x => x.ColumnValue == p));
if(myResult == null) myResult = "Four";
Edit Enumerable.Join is a very efficient hash table method, it also needs only one query:
string myResult = prefs.Select((pref, index) => new { pref, index })
.Join(myTable, xPref => xPref.pref, x => x.ColumnValue, (xPref, x) => new { xPref, x })
.OrderBy(x => x.xPref.index)
.Select(x => x.x.ColumnValue)
.DefaultIfEmpty("Four")
.First();
Demo
I wrote an extension method that effectively mirrors Tim Schmelter's answer (was testing this when he posted his update. :-()
public static T PreferredFirst<T>(this IEnumerable<T> data, IEnumerable<T> queryValues, T whenNone)
{
var matched = from d in data
join v in queryValues.Select((value,idx) => new {value, idx}) on d equals v.value
orderby v.idx
select new { d, v.idx };
var found = matched.FirstOrDefault();
return found != null ? found.d : whenNone;
}
// usage:
myResult = myTable.Select(x => x.ColumnValue)
.PreferredFirst(new [] {"Three", "One", "Two"}, "Four");
I've written one that will quit a little more early:
public static T PreferredFirst<T>(this IEnumerable<T> data, IList<T> orderBy, T whenNone)
{
// probably should consider a copy of orderBy if it can vary during runtime
var minIndex = int.MaxValue;
foreach(var d in data)
{
var idx = orderBy.IndexOf(d);
if (idx == 0) return d; // best case; quit now
if (idx > 0 && idx < minIndex) minIndex = idx;
}
// return the best found or "whenNone"
return minIndex == int.MaxValue ? whenNone : orderBy[minIndex];
}
I use a weighted approach in SQL where I assign a weight to each conditional value. The solution would then be found by finding the highest or lowest weight depending on your ordering scheme.
Below would be the equivalent LINQ query. Note that in this example I am assigning a lower weight a higher priority:
void Main()
{
// Assume below list is your dataset
var myList =new List<dynamic>(new []{
new {ColumnKey=1, ColumnValue ="Two"},
new {ColumnKey=2, ColumnValue ="Nine"},
new {ColumnKey=3, ColumnValue ="One"},
new {ColumnKey=4, ColumnValue ="Eight"}});
var result = myList.Select(p => new
{
ColVal = p.ColumnValue,
OrderKey = p.ColumnValue == "Three" ? 1 :
p.ColumnValue == "One" ? 2 :
p.ColumnValue == "Two" ? 3 : 4
}).Where(i=> i.OrderKey != 4)
.OrderBy(i=>i.OrderKey)
.Select(i=> i.ColVal)
.FirstOrDefault();
Console.WriteLine(result ?? "Four");
}
How about something like this:
var results = myTable.GroupBy(x => x.ColumnValue).ToList();
if (results.Contains("Three")) {
myResult = "Three";
} else if (results.Contains("One")) {
myResult = "One";
} else if (results.Contains("Two")) {
myResult = "Two";
} else {
myResult = "Four";
}

Grouping each 500 elements of array according to array elements

I have an array of 2000 strings. The strings are: "art", "economy", "sport" and "politic". I want to group each 500 elements and get their counts
Could anyone help please?
Another solution:
var count = 0;
var dictionaries =
strings.GroupBy(s => count++ / 500)
.Select(g => g.Distinct().ToDictionary(k => k, k => g.Count(s => s == k)))
.ToList();
This will create a List<Dictionary<string, int>>. Each dictionary represents a tally of 500 elements (or possibly less for the last dictionary), where the keys are strings and the values are the number of occurrences of the string among the 500 elements the dictionary represents.
There is no requirement to hardcode all the possible values that may be encountered.
For the maximum possible performance you can also use this version:
var count = 0;
var dictionaries =
strings.GroupBy(s => count++ / 500)
.Select(g => g.Aggregate(
new Dictionary<string, int>(),
(d, w) => { d[w] = (d.ContainsKey(w) ? d[w] + 1 : 1); return d; })
)
.ToList();
This version iterates over each element in your source array exactly once. The output is in the same format as the first version.
var result = strings.Select((s, i) => new { s, i })
.GroupBy(x => x.i / 500)
.Select(x => x.GroupBy(y => y.s)
.Select(z => new {
Name=z.Key,
Count=z.Count()
}).ToList())
.ToList();
Try
var grouping = Enumerable.Range(0,2000)
.Select(i => i / 500)
.Zip(Strings, (i,s) => new { Group = i, Str = s})
.GroupBy(anon => anon.Group,
anon => anon.Str,
(key,g) => new
{
Key = key,
Art = g.Count(str => str == "art"),
Economy = g.Count(str => str == "economy"),
Politic = g.Count(str => str == "politic"),
Sport= g.Count(str => str == "sport")
});
foreach(anon in grouping)
{
//textbox logic OP will have to change to suit
TextBox1.WriteLine(String.Format("Group: {0}", anon.Key));
TextBox1.WriteLine(String.Format("Art: {0}",anon.Art));
TextBox1.WriteLine(String.Format("Economy: {0}",anon.Economy ));
TextBox1.WriteLine(String.Format("Politic: {0}",anon.Politic ));
TextBox1.WriteLine(String.Format("Sport: {0}",anon.Sport));
}
Alternatively (as per Snowbear)
var grouping = Strings.Select((s,i) => new { Group = i / 500, Str = s})
.GroupBy(anon => anon.Group,
anon => anon.Str,
(key,g) => new
{
Key = key,
Art = g.Count(str => str == "art"),
Economy = g.Count(str => str == "economy"),
Politic = g.Count(str => str == "politic"),
Sport= g.Count(str => str == "sport")
});
foreach(anon in grouping)
{
//textbox logic OP will have to change to suit
TextBox1.WriteLine(String.Format("Group: {0}",anon.Key + 1));
TextBox1.WriteLine(String.Format("Art: {0}",anon.Art));
TextBox1.WriteLine(String.Format("Economy: {0}",anon.Economy ));
TextBox1.WriteLine(String.Format("Politic: {0}",anon.Politic ));
TextBox1.WriteLine(String.Format("Sport: {0}",anon.Sport));
}
int CountElementsInGroup = 500;
//from 500 to 1000
int NumberGroup = 2;
string[] GroupTypes = new string[4] { "art", "economy", "sport", "politic" };
//Fill example array
string[] arr = new string[2000];
Random rand = new Random();
for (int i = 0; i < arr.Length;i++ )
arr[i] = GroupTypes[rand.Next(0, 3)];
var res = (from p in arr.Skip((NumberGroup - 1) * CountElementsInGroup).Take(CountElementsInGroup)
group p by p into g
select new GroupCountClass { GroupName = g.Key, GroupCount = g.Count() });
textBox1.Text = "";
foreach (GroupCountClass c in res)
{
textBox1.Text += String.Format("GroupName:{0} Count:{1};",c.GroupName,c.GroupCount);
}

Categories

Resources