Linq: Sum() non-integer values - c#

This is a continuation from my previos question:
Linq (GroupBy and Sum) over List<List<string>>
I have a query like so:
var content = new List<List<string>>
{
new List<string>{ "book", "code", "columnToSum" },
new List<string>{ "abc", "1", "10" },
new List<string>{ "abc", "1", "5" },
new List<string>{ "cde", "1", "6" },
};
var headers = content.First();
var result = content.Skip(1)
.GroupBy(s => new { Code = s[headers.IndexOf("code")], Book = s[headers.IndexOf("book")]})
.Select(g => new
{
Book = g.Key.Book,
Code = g.Key.Code,
Total = g.Select(s => int.Parse(s[headers.IndexOf("columnToSum")])).Sum()
});
This works fine but I'm just wondering how I can handle the case there the columnToSum is empty? So for example this gives me the error "Input string was not in a correct format" as the int.Parse fails
var content = new List<List<string>>
{
new List<string>{ "book", "code", "columnToSum" },
new List<string>{ "abc", "1", "10" },
new List<string>{ "abc", "1", "" },
new List<string>{ "cde", "1", "6" },
};
How can I handle this scenario gracefully?

Why don't you just add a zero onto the front of the string?
s => int.Parse("0" + s[headers.IndexOf("columnToSum")])
Of course, it's a big hack. But it will solve your problem quickly and (quite) readably if the only exceptional case you're really worried about is the empty string.
I wonder where you're getting these empty strings from. If it's something you have control over like a SQL query, why don't you just change your query to give "0" for no value? (As long as the empty column isn't used in a different sense somewhere else in your code.)

One option, use string.All(Char.IsDigit) as pre-check:
Total = g.Select(s => !string.IsNullOrEmpty(s[headers.IndexOf("columnToSum")]) &&
s[headers.IndexOf("columnToSum")].All(Char.IsDigit) ?
int.Parse(s[headers.IndexOf("columnToSum")]) : 0).Sum())
another would be to use int.TryParse:
int val = 0;
// ...
Total = g.Select(s => int.TryParse(s[headers.IndexOf("columnToSum")], out val) ?
int.Parse(s[headers.IndexOf("columnToSum")]) : 0).Sum())

That code assumes that empty string is 0:
Total = g.Where(s => !String.IsNullOrEmpty(s)).Select(s => int.Parse(s[headers.IndexOf("columnToSum")])).Sum()

Unfortunately, this isn't going to look very nice...
g.Select(s => !s[headers.IndexOf("columnToSum")].Any(Char.IsDigit) ?
0 : Int32.Parse(s[headers.IndexOf("columnToSum")])).Sum()
However, you could wrap this up in a nice extension method
public static class StrExt
{
public static int IntOrDefault(this string str, int defaultValue = 0)
{
return String.IsNullOrEmpty(str) || !str.Any(Char.IsDigit) ? defaultValue : Int32.Parse(str);
}
}
...
g.Select(s => s[headers.IndexOf("columnToSum")].IntOrDefault()).Sum();
The extension method give you the flexibility to set whatever default value you want if the str is not a number - it defaults to 0 if the parameter is ommitted.

Using lists here is problematic, and I would parse this into a proper data structure (like a Book class), which I think will clean up the code a bit. If you're parsing CSV files, take a look at FileHelpers, it's great library for these types of tasks, and it can parse into a data structure for you.
That being said, if you'd still like to continue using this paradigm, I think you can get the code fairly clean by creating two custom methods: one for dealing with the headers (one of the few places I'd use dynamic types to get rid of ugly strings in your code) and one for parsing the ints. You then get something like this:
var headers = GetHeaders(content.First());
var result = from entry in content.Skip(1)
group entry by new {Code = entry[headers.code], Book = entry[headers.book] } into grp
select new {
Book = grp.Key.Book,
Code = grp.Key.Code,
Total = grp.Sum(x => ParseInt(x[headers.columnToSum]))
};
public dynamic GetHeaders(List<string> headersList){
IDictionary<string, object> headers = new ExpandoObject();
for (int i = 0; i < headersList.Count; i++)
headers[headersList[i]] = i;
return headers;
}
public int ParseInt(string s){
int i;
if (int.TryParse(s, out i))
return i;
return 0;
}

You can use multiple lines in a lambda expression and return a value at end.
So, instead of
Total = g.Select(s => int.Parse(s[headers.IndexOf("columnToSum")])).Sum()
I would write
Total = g.Select(s => {
int tempInt = 0;
int.TryParse(s[headers.IndexOf("columnToSum")], out tempInt);
return tempInt;
}).Sum()

t = new List<List<string>>
{
new List<string>{ "book", "code", "columnToSum" },
new List<string>{ "abc", "1", "10" },
new List<string>{ "abc", "1", "5" },
new List<string>{ "cde", "1", "6" },
};
var headers = content.First();
var result = content.Skip(1)
.GroupBy(s => new { Code = s[headers.IndexOf("code")], Book = s[headers.IndexOf("book")]})
.Select(g => new
{
Book = g.Key.Book,
Code = g.Key.Code,
Total = g.Select(s => int.Parse(s[headers.IndexOf("columnToSum")]!=""?s[headers.IndexOf("columnToSum")]:0)).Sum()
});

Related

How to choose one right data type from several detected

I'm working on one problem. I have a list<string> of detected data types ("int", "double", "string", "bool", "datetime", "timespan", "datetimeoffset").
Now I need to choose something like "default" one data type that will be used for all values in array. How to create (theoretically) the logic to setting the appropriate data type?
For example if was detected at least one string, default data type will be defined as string, because this type can "store" also other data types, such as bool or date.
Name
If i understand what you want correctly, you can make a method like this :
public static List<object> GetMostLikelyType(List<string> inputs)
{
List<object> result = new List<object>() ;
int num;
double d;
DateTime dt;
bool b;
TimeSpan ts;
DateTimeOffset dto;
if (inputs.All(i => int.TryParse(i, out num)))
result = inputs.Select(x => (object)int.Parse(x)).ToList();
else if (inputs.All(i => double.TryParse(i, out d)))
result = inputs.Select(x => (object)double.Parse(x)).ToList();
else if (inputs.All(i => DateTime.TryParse(i, out dt)))
result = inputs.Select(x => (object)DateTime.Parse(x)).ToList();
else if (inputs.All(i => bool.TryParse(i, out b)))
result = inputs.Select(x => (object)bool.Parse(x)).ToList();
else if (inputs.All(i => TimeSpan.TryParse(i, out ts)))
result = inputs.Select(x => (object)TimeSpan.Parse(x)).ToList();
else if (inputs.All(i => DateTimeOffset.TryParse(i, out dto)))
result = inputs.Select(x => (object)DateTimeOffset.Parse(x)).ToList();
else
result = inputs.Select(x => (object)x.ToString()).ToList();
return result;
}
And then use it for your List (respective outputs is commented) :
List<string> strings = new List<string>() {"2016/7/3","2025/12/01" };
//List of DateTime objects
List<string> strings2 = new List<string>() { "25", "21.12" };
//List of Double objects
List<string> strings3 = new List<string>() { "true", "false" };
//List of bool objects
List<string> strings4 = new List<string>() { "12", "0" };
//List of int objects
List<string> strings5 = new List<string>() { (new TimeSpan(2,3,3)).ToString(), "0" };
//List of TimeSpan objects
List<string> strings6 = new List<string>() { "2016/7/3" , "3"};
//string
var result = GetMostLikelyType(strings);
var result2 = GetMostLikelyType(strings2);
var result3 = GetMostLikelyType(strings3);
var result4 = GetMostLikelyType(strings4);
var result5 = GetMostLikelyType(strings5);
var result6 = GetMostLikelyType(strings6);
You could declare everything as an object since object is the base type for everything.
then you iterate through the list and test with typeof.
list<object> tt
in your loop use :
if (tt[I].GetType() == typeof(Integer))
I prefer use a list of Object or list of dynamic:
List<object> stuff = new List<object>();
stuff.add("test");
stuff.add(35);
Console.WriteLine((string)stuff[0]);
Console.WriteLine((int)stuff[1]);
and here is dynamic example:
var list = new List<dynamic>();
list.Add(123);
list.Add(new
{
Name = "Lorem Ipsum"
});

Get the element with in the array whose occurrence is 4 times

I have an array of strings , i want to find those element whose occurrence are 4 or more than 4 times with in the array.
my code
internal static void ProcessArray(string[] numArray)
{
string response = string.Empty;
var duplicates = (numArray
.GroupBy(e => e)
.Where(e => e.Count() >= 4)
.Select(e => e.First())).ToList();
//do some further business logic
}
So duplicate should return me a list of string which has the element.
I am calling this from my method below
Public static string GetDuplicates()
{
string[] s = new new string[]{" 1","1","2","2","2","1","3,"2","1" }
string result = ProcessArray(s);
return result
}
it only returns 2 in the list , the correct result should be 1,2 in the list.
var values = new string [] { "1", "1", "2", "2", "2", "1", "3", "2", "1" };
var groups = values.GroupBy(i => i).Select(i => new { Number = i.Key, Count = i.Count() });
foreach(var item in groups)
{
if(item.Count == 4)
{
Console.WriteLine(item.Number);
}
}
WORKING FIDDLE

Using LINQ To Interleave Two Lists

So here is what I'm attempting to do:
symbolList: 1,1,1,2,2,2
valueList: a1,b1,c1,a2,b2,c2
result: 1|a1|b1|c1,2|a2|b2|c2
The a,b,c for each symbol are all different values
For each symbol there will be the same amount of corresponding values. So for symbol 1, there are three values that I would like joined with it. For symbol 2 I want the second set of three values joined to it, etc.
However, with my current implementation, I currently get:
result: 1|a1|b1|c1|a2|b2|c2, 2|a1|b1|c1|a2|b2|c2
Here's my current code:
List<string> results = symbolList.Distinct().Select(f =>
String.Join("|", new List<string>() { f }.Concat(valueList))).ToList();
string value = string.Join(",", results);
I've attempted to use group by in some fashion, but haven't found a viable result doing that. Any help is greatly appreciated.
From your description it looks like the paired index matters, you are not trying to do a blind interleave. Here is how to do it with a GroupBy, I also put a 3 in the middle to demonstrate the ordering.
List<string> symbolList = new List<string>() { "1", "1", "3", "2", "2", "2" };
List<string> valueList = new List<string>() { "a1", "b1", "c3", "a2", "b2", "c2" };
var items = symbolList.Zip(valueList, (symbol, value) => new {symbol, value}) //pairs the like indexes with each other in to an anonymous type.
.GroupBy(pair => pair.symbol, pair => pair.value) //Group together the values that share the same symbol
.OrderBy(group => group.Key) //optional, only if you want the output to be 1,2,3
.Select(group => String.Join("|", (new[] {group.Key}).Concat(group))); //Build up the "|" separated groups.
var result = String.Join(",", items); //Join together the groupings separated by ","
Console.WriteLine("result: " + result);
Click to Run
It gives the result result: 1|a1|b1,2|a2|b2|c2,3|c3
Can you please try this out:
List<string> symbolList = new List<string>() { "1", "1", "1", "2", "2", "2" };
List<string> valueList = new List<string>() { "a", "b", "c", "a", "b", "c" };
string value = string.Join(",", symbolList.Distinct().
Select(f => String.Join("|", new List<string>() { f }
.Concat(valueList.Distinct()))).ToList());
Console.WriteLine("result: " + value);
This code gives me result: "1|a|b|c,2|a|b|c".
I hope this is what you want.
While not the answer to the original question, to strictly answer the title (which is what I came here looking for, just simply interleaving two lists):
var interleaved = list1.Zip(list2, (a, b) => new[] { a, b }).SelectMany(p => p);
Example:
var l1 = Enumerable.Range(0, 10).Select(i => (char) ('0' + i));
var l2 = Enumerable.Range(0, 10).Select(i => (char) ('A' + i));
var interleaved = l1.Zip(l2, (a, b) => new[] { a, b }).SelectMany(p => p);

C# GroupBy - Creating multiple grouping levels

Given the following class:
public class Transaction
{
public string Category { get; set; }
public string Form { get; set; }
}
How do I get a grouping of transactions that are grouped by both the Category and the Form?
Basically I want it to output like so:
Category 1
Form 1
Transaction1
Transaction2
Transaction3
...
Form 2
Transaction1
Transaction2
Transaction3
...
Category 2
Form 1
Transaction1
Transaction2
Transaction3
...
Form 2
Transaction1
Transaction2
Transaction3
...
Here is an example using nested foreach loops, I'm not sure how you would do this in a single string of linq statements, maybe with lots of selectmanys?
var transactions = new[]{
new{Category = "1", Form = "1", Title = "Trans1" },
new{Category = "1", Form = "1", Title = "Trans2" },
new{Category = "1", Form = "1", Title = "Trans3" },
new{Category = "1", Form = "2", Title = "Trans1" },
new{Category = "1", Form = "2", Title = "Trans2" },
new{Category = "1", Form = "2", Title = "Trans3" },
new{Category = "2", Form = "1", Title = "Trans1" },
new{Category = "2", Form = "1", Title = "Trans2" },
new{Category = "2", Form = "1", Title = "Trans3" },
new{Category = "1", Form = "3", Title = "Trans1" },
new{Category = "1", Form = "3", Title = "Trans2" },
new{Category = "1", Form = "3", Title = "Trans3" },
};
foreach(var byCategory in transactions.GroupBy(x => x.Category))
{
Console.WriteLine(byCategory.Key);
foreach(var byForm in byCategory.GroupBy(x => x.Form))
{
Console.WriteLine("\t" + byForm.Key);
foreach(var trans in byForm)
{
Console.WriteLine("\t\t" + trans.Title);
}
}
}
Just because I was curious what it would look like I came up with the following, YOU SHOULD NOT USE THIS IN PRODUCTION CODE as it is ridiculous (if you do have a data structure like this it should be broken up into something like Dictionary<CategoryName, FormGroup> or something with meaningful types)
Dictionary<string, Dictionary<string, List<string>>> tooManyDictionaries = transactions
.GroupBy(x => x.Category)
.ToDictionary(
catGroup => catGroup.Key,
catGroup => catGroup
.GroupBy(x => x.Form)
.ToDictionary(
formGroup => formGroup.Key,
formGroup => formGroup.Select(x => x.Title).ToList()));
I ended up with the following, because the grouping need to be complete before the iteration over the collection.
Seed Some Transactions
var cats = new[] { "Category 1", "Category 2", "Category 3" };
var frms = new[] { "Form 1", "Form 2", "Form 3" };
var transactions = new List<Transaction>();
for (var i = 0; i <= 150; i++)
{
transactions.Add(new Transaction
{
Category = i % 2 == 0 ? cats[0] : i % 3 == 0 ? cats[1] : cats[2],
Form = i % 5 == 0 ? frms[0] : i % 7 == 0 ? frms[1] : frms[2]
});
}
The Grouping
var groupedTransactions = transactions.GroupBy(x => x.Category)
.Select(x => new
{
Category = x.Key,
Forms = x.ToList()
.GroupBy(y => y.Form)
});
Write it to the Console
foreach (var group in groupedTransactions.OrderBy(x => x.Category))
{
Console.WriteLine(group.Category);
foreach (var form in group.Forms.OrderBy(x => x.Key))
{
Console.WriteLine("\t" + form.Key);
foreach (var transaction in form)
{
Console.WriteLine("\t\t" + transaction.Id);
}
}
}
Basically, the first group should contain every piece of information that you will need to group by later, and then on repeat groupings, remove one piece of information that is relevant for that grouping level. Do this as many times as you need.
ValueTuple can help a lot, since it lets you have a composite key that can be passed to another type. Otherwise with anonymous types, you'll need to rely on type inference to pass the groups to something else. One issue with ValueTuple though is that you can't have a 1-Tuple for some reason, so in that case you need to group by the single property and not use a tuple.
If you already have a hierarchical relationship in your data structure, then grouping by a Tuple might be unnecessary.
var groups =
transactions
.GroupBy(tran => (
Category: tran.Category,
Form: tran.Form
)).GroupBy(group => group.Key.Form)
.ToList();
The type gets complicated very fast, so use type inference and refactoring tools to avoid having to figure out the specific type, when possible. For example, just the above results in the type:
List<IGrouping<string, IGrouping<(string Category, string Form), Transaction>>>

LINQ-to-objects index within a group + for different groupings (aka ROW_NUMBER with PARTITION BY equivalent)

After much Google searching and code experimentation, I'm stumped on a complex C# LINQ-to-objects problem which in SQL would be easy to solve with a pair of ROW_NUMBER()...PARTITION BY functions and a subquery or two.
Here's, in words, what I'm trying to do in code-- the underlying requirement is removing duplicate documents from a list:
First, group a list by (Document.Title, Document.SourceId), assuming a (simplified) class definition like this:
class Document
{
string Title;
int SourceId; // sources are prioritized (ID=1 better than ID=2)
}
Within that group, assign each document an index (e.g. Index 0 == 1st document with this title from this source, Index 1 = 2nd document with this title from this source, etc.). I'd love the equivalent of ROW_NUMBER() in SQL!
Now group by (Document.Title, Index), where Index was computed in Step #2. For each group, return only one document: the one with the lowest Document.SourceId.
Step #1 is easy (e.g. codepronet.blogspot.com/2009/01/group-by-in-linq.html), but I'm getting stumped on steps #2 and #3. I can't seem to build a red-squiggle-free C# LINQ query to solve all three steps.
Anders Heilsberg's post on this thread is I think the answer to Steps #2 and #3 above if I could get the syntax right.
I'd prefer to avoid using an external local variable to do the Index computation, as recommended on slodge.blogspot.com/2009/01/adding-row-number-using-linq-to-objects.html, since that solution breaks if the external variable is modified.
Optimally, the group-by-Title step could be done first, so the "inner" groupings (first by Source to compute the index, then by Index to filter out duplicates) can operate on small numbers of objects in each "by title" group, since the # of documents in each by-title group is usually under 100. I really don't want an N2 solution!
I could certainly solve this with nested foreach loops, but it seems like the kind of problem which should be simple with LINQ.
Any ideas?
I think jpbochi missed that you want your groupings to be by pairs of values (Title+SourceId then Title+Index). Here's a LINQ query (mostly) solution:
var selectedFew =
from doc in docs
group doc by new { doc.Title, doc.SourceId } into g
from docIndex in g.Select((d, i) => new { Doc = d, Index = i })
group docIndex by new { docIndex.Doc.Title, docIndex.Index } into g
select g.Aggregate((a,b) => (a.Doc.SourceId <= b.Doc.SourceId) ? a : b);
First we group by Title+SourceId (I use an anonymous type because the compiler builds a good hashcode for the grouping lookup). Then we use Select to attach the grouped index to the document, which we use in our second grouping. Finally, for each group we pick the lowest SourceId.
Given this input:
var docs = new[] {
new { Title = "ABC", SourceId = 0 },
new { Title = "ABC", SourceId = 4 },
new { Title = "ABC", SourceId = 2 },
new { Title = "123", SourceId = 7 },
new { Title = "123", SourceId = 7 },
new { Title = "123", SourceId = 7 },
new { Title = "123", SourceId = 5 },
new { Title = "123", SourceId = 5 },
};
I get this output:
{ Doc = { Title = ABC, SourceId = 0 }, Index = 0 }
{ Doc = { Title = 123, SourceId = 5 }, Index = 0 }
{ Doc = { Title = 123, SourceId = 5 }, Index = 1 }
{ Doc = { Title = 123, SourceId = 7 }, Index = 2 }
Update: I just saw your question about grouping by Title first. You can do this using a subquery on your Title groups:
var selectedFew =
from doc in docs
group doc by doc.Title into titleGroup
from docWithIndex in
(
from doc in titleGroup
group doc by doc.SourceId into idGroup
from docIndex in idGroup.Select((d, i) => new { Doc = d, Index = i })
group docIndex by docIndex.Index into indexGroup
select indexGroup.Aggregate((a,b) => (a.Doc.SourceId <= b.Doc.SourceId) ? a : b)
)
select docWithIndex;
To be honest, I'm quite confused with your question. Maybe if you should explain what you're trying to solve. Anyway, I'll try to answer what I understood.
1) First, I'll assume that you already have a list of documents grouped by Title+SourceId. For testing purposes, I hardcoded a list as follow:
var docs = new [] {
new { Title = "ABC", SourceId = 0 },
new { Title = "ABC", SourceId = 4 },
new { Title = "ABC", SourceId = 2 },
new { Title = "123", SourceId = 7 },
new { Title = "123", SourceId = 5 },
};
2) To get put a index in every item, you can use the Select extension method, passing a Func selector function. Like this:
var docsWithIndex
= docs
.Select( (d, i) => new { Doc = d, Index = i } );
3) From what I understood, the next step would be to group the last result by Title. Here's how to do it:
var docsGroupedByTitle
= docsWithIndex
.GroupBy( a => a.Doc.Title );
The GroupBy function (used above) returns an IEnumerable<IGrouping<string,DocumentWithIndex>>. Since a group is enumerable too, we now have an enumerable of enumerables.
4) Now, for each of the groups above, we'll get only the item with the minimum SourceId. To make this operation we'll need 2 levels of recursion. In LINQ, the outer level is a selection (for each group, get one of its items), and the inner level is an aggregation (get the item with the lowest SourceId):
var selectedFew
= docsGroupedByTitle
.Select(
g => g.Aggregate(
(a, b) => (a.Doc.SourceId <= b.Doc.SourceId) ? a : b
)
);
Just to ensure that it works, I tested it with a simple foreach:
foreach (var a in selectedFew) Console.WriteLine(a);
//The result will be:
//{ Doc = { Title = ABC, SourceId = 0 }, Index = 0 }
//{ Doc = { Title = 123, SourceId = 5 }, Index = 4 }
I'm not sure that's what you wanted. If not, please comment the answer and I can fix the answer. I hope this helps.
Obs.: All the classes used in my tests were anonymous. So, you don't really need to define a DocumentWithIndex type. Actually, I haven't even declared a Document class.
Method Based Syntax:
var selectedFew = docs.GroupBy(doc => new {doc.Title, doc.SourceId}, doc => doc)
.SelectMany((grouping) => grouping.Select((doc, index) => new {doc, index}))
.GroupBy(anon => new {anon.doc.Title, anon.index})
.Select(grouping => grouping.Aggregate((a, b) => a.doc.SourceId <= b.doc.SourceId ? a : b));
Would you say the above is the equivalent Method based syntax?
I implemented an extension method. It supports multiple partition by fields as well as multiple order conditions.
public static IEnumerable<TResult> Partition<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<IEnumerable<TSource>, IOrderedEnumerable<TSource>> sorter,
Func<TSource, int, TResult> selector)
{
AssertUtilities.ArgumentNotNull(source, "source");
return source
.GroupBy(keySelector)
.Select(arg => sorter(arg).Select(selector))
.SelectMany(arg => arg);
}
Usage:
var documents = new[]
{
new { Title = "Title1", SourceId = 1 },
new { Title = "Title1", SourceId = 2 },
new { Title = "Title2", SourceId = 15 },
new { Title = "Title2", SourceId = 14 },
new { Title = "Title3", SourceId = 100 }
};
var result = documents
.Partition(
arg => arg.Title, // partition by
arg => arg.OrderBy(x => x.SourceId), // order by
(arg, rowNumber) => new { RowNumber = rowNumber, Document = arg }) // select
.Where(arg => arg.RowNumber == 0)
.Select(arg => arg.Document)
.ToList();
Result:
{ Title = "Title1", SourceId = 1 },
{ Title = "Title2", SourceId = 14 },
{ Title = "Title3", SourceId = 100 }

Categories

Resources