Convert List<dynamic> to List using LINQ - c#

I'm having a collection List<dynamic> dList. In that, it has string items and List<string> items. Now I need to organize all the values in a single List.
Just refer the List<dynamic> dList
Case: 1
List<dynamic> dList = new List<dynamic>()
{
"Selva",
new List<string>() {"Bala"},
new List<string>() {"Prayag", "Raj"},
"Pavithran"
};
Case: 2
List<object> bala = new List<dynamic>()
{
"Selva",
new List<object>() {"Bala"},
new List<object>() {"Prayag", "Raj"},
"Pavithran"
};
The Output of the above two List are
My Expected Output is
How could I achieve the expected result from the above List<dynamic>? The List is generated at run time and I cannot to change the structure.
This is a small part of a complex Linq query, so, I need to achieve this in Linq.

If order is important then you can convert every element to a List<string> and then flatten these:
List<dynamic> dList = new List<dynamic>()
{
"Selva",
new List<string>() {"Bala"},
new List<string>() {"Prayag", "Raj"},
"Pavithran"
};
var flattenedList = dList.SelectMany(d =>
{
if (d is string)
{
return new List<string>() { d };
}
else if (d is List<string>)
{
return (d as List<string>);
}
else
{
throw new Exception("Type not recognised");
}
});
Or, as a sexy one-liner with no type-checking (so...use at your own risk!)
dList.SelectMany(d => d as List<string> ?? new List<string>() { d })
Or, finally, in LINQ syntax:
var newList =
(from d in dList
from d2 in EnsureListOfString((object)d)
select d2
);
public List<string> EnsureListOfString(object arg)
{
List<string> rtn = arg as List<string>;
if (rtn == null)
{
if (arg is string)
{
rtn = new List<string>() { arg as string };
}
else
{
throw new Exception("Type not recognised.");
}
}
return rtn;
}

If the order of the elements is not important, you can do this:
dList.OfType<string>().Concat(dList.OfType<List<string>>().SelectMany(l => l));
This first selects all string elements from the list, then selects all List<string> elements and flattens them using SelectMany and finally concats all strings.

Related

IEnumerable.Select() when attribute is known only at runtime

Say I have a data class like this and a list of its objects:
public class DataSet
{
public int A { get; set; }
public string B { get; set; }
public double C { get; set; }
}
var data = new List<DataSet>
{
new DataSet() { A = 1, B = "One", C = 1.1 },
new DataSet() { A = 2, B = "Two", C = 2.2 },
new DataSet() { A = 3, B = "Three", C = 3.3 }
};
I would like to do a Select() on the list, based on different properties. For example, if I need a list of property A, I could do this easily:
var listA = data.Select(x => x.A).ToList();
All good so far.
But in my program, I need to do the above, only, I wouldn't know whether I need a list of A or B or C until runtime. This 'knowledge' of what to select is stored in a list of strings, and I need to iterate it and extract only the appropriate lists. Something like this:
// GetKeys() will return the keys that I need to extract.
// So at one time keyList could have "A" and "B", another time "B" and "C" etc.
List<string> keyList = GetKeys();
foreach (var key in keyList)
{
// What do I do here?
data.Select(x =>???).ToList();
}
Is this possible at all? I'm fine with even a non-LINQ solution, if it achieves my goal.
EDIT:
Clarifying the requirement.
The end result I want is a separate list based on each 'key' mentioned above. So, something like
List<List<object>>
The count in outer list would be the count of keyList.
The inner list would have as many items as in DataSet.
This would probably not be the most efficient solution, but you could use Reflection for a fully dynamic solution:
private static List<List<object>> SelectDynamicData<T>(IEnumerable<T> data, List<string> properties)
{
// get the properties only once per call
// this isn't fast
var wantedProperties = typeof(T)
.GetProperties()
.Where(x => properties.Contains(x.Name))
.ToArray();
var result = new Dictionary<string, List<object>>();
foreach (var item in data)
{
foreach (var wantedProperty in wantedProperties)
{
if (!result.ContainsKey(wantedProperty.Name))
{
result.Add(wantedProperty.Name, new List<object>());
}
result[wantedProperty.Name].Add(wantedProperty.GetValue(item));
}
}
return result.Select(x => x.Value).ToList();
}
And, of course, you'd need to do a double foreach or a LINQ query to print that. For example:
var data = new List<DataSet>
{
new DataSet() { A = 1, B = "One", C = 1.1 },
new DataSet() { A = 2, B = "Two", C = 2.2 },
new DataSet() { A = 3, B = "Three", C = 3.3 }
};
var selectedData = SelectDynamicData(data, new List<string> { "A", "C" });
foreach (var list in selectedData)
{
foreach (object item in list)
{
Console.Write(item + ", ");
}
Console.WriteLine();
}
Using Creating Expression Trees by Using the API you can build an expression tree to represent the linq query you were hard coding in order to make it more dynamic.
Expression<Func<TModel, object>> GetPropertyExpression<TModel>(string propertyName) {
// Manually build the expression tree for
// the lambda expression v => v.PropertyName.
// (TModel v) =>
var parameter = Expression.Parameter(typeof(TModel), "v");
// (TModel v) => v.PropertyName
var property = Expression.Property(parameter, propertyName);
// (TModel v) => (object) v.PropertyName
var cast = Expression.Convert(property, typeof(object));
var expression = Expression.Lambda<Func<TModel, object>>(cast, parameter);
return expression;
}
Review the comments to understand the building of the expression tree.
This now can be used with the data to extract the desired result.
Following similar to what was provided in another answer it would be simplified to
List<List<object>> SelectDynamicData<T>(IEnumerable<T> data, List<string> properties) {
return properties
.Select(_ => data.Select(GetPropertyExpression<T>(_).Compile()).ToList())
.ToList();
}
Both methods are displayed in the following example
[TestMethod]
public void TestMethod1() {
var data = new List<DataSet>
{
new DataSet() { A = 1, B = "One", C = 1.1 },
new DataSet() { A = 2, B = "Two", C = 2.2 },
new DataSet() { A = 3, B = "Three", C = 3.3 }
};
var propertyKnownAtRuntime = "A";
var expression = GetPropertyExpression<DataSet>(propertyKnownAtRuntime);
var listA = data.Select(expression.Compile()).ToList();
//Produces
// { 1, 2, 3}
var listAC = SelectDynamicData(data, new List<string> { "A", "C" });
//Produces
//{
// { 1, 2, 3},
// { 1.1, 2.2, 3.3 }
//}
}
You can use reflection, for example
string key = "A";
var query = data.Select(x =>
{
var prop = x.GetType().GetProperty(key); //NOTE: if key does not exist this will return null
return prop.GetValue(x);
});
foreach (var value in query)
{
Console.WriteLine(value); //will print 1, 2, 3
}

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"
});

Concat all strings inside a List<List<string>> using LINQ

My question is almost same as this one, but the List dimension is n.
How to concat all strings inside a List<List<List...<string>> (n dimension list) using LINQ ?
NOTE: Interested for both cases, n is known or unknown
Since the linked question is tagged as c# so i add this answer with c# code.
If the number of nested lists are known You have to use SelectMany() over and over to unwrap all the nested lists to sequence of chars. then make string out of that sequence.
List<List<List<string>>> nestedList = new List<List<List<string>>>();
var result = new string(nestedList.SelectMany(x => x).SelectMany(x => x).SelectMany(x => x).ToArray());
If the number of nested lists are not known you have to use reflection, since the type is not known. I didnt used reflection directly but actually dynamic type does. The performance would be terrible here of course ;) but it does what you want.
using Microsoft.CSharp.RuntimeBinder;
//...
private static string ConcatAll<T>(T nestedList) where T : IList
{
dynamic templist = nestedList;
try
{
while (true)
{
List<dynamic> inner = new List<dynamic>(templist).SelectMany<dynamic, dynamic>(x => x).ToList();
templist = inner;
}
}
catch (RuntimeBinderException)
{
List<object> l = templist;
return l.Aggregate("", (a, b) => a + b);
}
}
Here is the test
private static void Main(string[] args)
{
List<List<List<string>>> nestedList = new List<List<List<string>>>
{
new List<List<string>> {new List<string> {"Hello "}, new List<string> {"World "}},
new List<List<string>> {new List<string> {"Goodbye "}, new List<string> {"World ", "End "}}
};
Console.WriteLine(ConcatAll(nestedList));
}
Outputs:
Hello World Goodbye World End
Update:
After a bit fiddling i ended up this implementation. maybe better without try catch.
private static string ConcatAll<T>(T nestedList) where T : IList
{
dynamic templist = nestedList;
while (templist.Count > 0 && !(templist[0] is char?))
{
List<dynamic> inner = new List<dynamic>(templist).SelectMany<dynamic, dynamic>(x =>
{
var s = x as string;
if (s != null)
{
return s.Cast<dynamic>();
}
return x;
}).ToList();
templist = inner;
}
return new string(((List<object>) templist).Cast<char>().ToArray());
}
Another solution could be using a recursive method to flatten all your lists:
static IEnumerable<string> Flatten(IEnumerable enumerable)
{
foreach (object el in enumerable)
{
if (enumerable is IEnumerable<string>)
{
yield return (string) el;
}
else
{
IEnumerable candidate = el as IEnumerable;
if (candidate != null)
{
foreach (string nested in Flatten(candidate))
{
yield return nested;
}
}
}
}
}
With this method you can concat all the strings this way:
List<List<List<string>>> nestedList = new List<List<List<string>>>
{
new List<List<string>> {new List<string> {"Hello "}, new List<string> {"World "}},
new List<List<string>> {new List<string> {"Goodbye "}, new List<string> {"World ", "End "}}
};
Console.WriteLine(String.Join(" ",Flatten(nestedList)));
This idea was taken from this post.

Combining lists in linq

in linq, is it possible to combine many lists (of the same type), such that two lists,
list 1 = {a,b,c} and list 2 = {x,y,z}
turns into {[1,a] , [1,b] , [1,c] , [2,x] , [2,y] , [2,z] }
where [] represents a pair containing a "list identifier"
The problem is from having decks of arbitrary cards, where each deck is a list in a collection of lists.
I'm trying to create a query such that I can select only cards in a certain deck, or cards similar to 2 or more decks.
This is probably a duplicate question, but I don't know how to search for the question further then I already have.
List<List<int>> lists;
var combined = lists.Select((l, idx) => new { List = l, Idx = idx })
.SelectMany(p => p.List.Select(i => Tuple.Create(p.Idx + 1, i)));
var list1 = new List<string>() {a,b,c};
var list2 = new List<string>() {x,y,z};
var combined = list1.Select(x => new { id = 1, v = x }).Concat(list2.Select(x => new { id = 2, v = x }));
Normally I'd suggest Enumerable.Zip for combining multiple lists, however you seem to actually want to concatenate multiple lists with a list counter.
public IEnumerable<Tuple<int,T>> Combine<T>(params IEnumerable<T>[] lists) {
return lists.Select((x,i) => x.Select(y => Tuple.Create(i+1,y))).SelectMany (l =>l);
}
UPDATE
Completely missed that SelectMany has the index option so the above code can be written as
public IEnumerable<Tuple<int,T>> Combine<T>(params IEnumerable<T>[] lists) {
return lists.SelectMany((x,i) => x.Select(y => Tuple.Create(i+1,y)));
}
Then you can do
var list1 = new List<string> { "a", "b", "c" };
var list2 = new List<string> { "x", "y", "z" };
var combined = Combine(list1,list2);
Combined will be enumerable of tuples, with Item1 being the list index identifier (starting at 1) and Item2 being the value.
This method will handle multiple lists so you could just as easily call it with:
var list3 = new List<string> { "f", "g" };
var combined = Combine(list1,list2,list3);
You can merge the lists like:
var first = new List<string> {"a","b","c"};
var second = new List<string> {"x","y","z"};
var merged = first.Select(item => new { ListIndex = 1, Value = item}).ToList();
merged.AddRange(second.Select(item => new { ListIndex = 2, Value = item});
//or use concat
var merged = first.Select(item => new { ListIndex = 1, Value = item});
.Concat(second.Select(item => new { ListIndex = 2, Value = item});
Alternatively if you have the sources in something like:
List<List<string>> lists = new List<List<string>>
{
new List<string> {"a","b","c"},
new List<string> {"x","y","z"}
};
you can do:
var merged = lists.SelectMany((item, index) =>
item.Select(s => new { ListIndex = index, Value = s}));
Note that this will produce a 0-based list, so if you really need a 1-base list, just do ListIndex = index +1.
Also, if you will use this a lot, I would create it as an specific entity, something like
struct ListIdentValue
{
public int ListIndex {get; private set;}
public string Value {get; private set;}
public ListIdentValue(int listIndex, string value) {...}
}
Try using Concat
new[] {'a','b','c'}
.Select(v=>new Tuple<int,char>(1, v))
.Concat(
new[] {'x','y','z'}.Select(v=>new Tuple<int,char>(2, v))
)
string[] a = { "a", "b", "c" };
string[] b = { "x", "z", "y" };
var t =
(
from ai in a
select new { listNo = 1, Item = ai }
).Union
(
from bi in b
select new { listNo = 2, Item = bi }
);
or
var t =
(
from ai in a
select new object[] { 1, ai }
).Union
(
from bi in b
select new object[] { 2, bi }
);

Check difference between 2 IEnumerables

So basically I have the following 2 IEnumerable lists
List A = {"Personal", "Tech", "Social"}
List B = {"Personal", "Tech", "General"}
Now what I want to achieve is, get the difference between List A and List B, in this case Social and General.
I also need to determine that Social is extra in List A and General is extra in List B to insert and delete accordingly.
I can also have another scenario
List A = {"Personal", "Tech"}
List B = {"Personal", "Tech", "General"}
in this case it would return General"
How can I do that with LINQ?
Here you go
var ListA = new List<string> {"Personal", "Tech", "Social"};
var ListB = new List<string> { "Personal", "Tech", "General" };
var insert = ListA.Except(ListB).ToList();
var delete = ListB.Except(ListA).ToList();
You can use List<T>.Except() Method.
Produces the set difference of two sequences.
public static void Main(string[] args)
{
List<string> A = new List<string> { "Personal", "Tech", "Social" };
List<string> B = new List<string> { "Personal", "Tech", "General" };
var result = A.Except(B);
//Will print "Social"
foreach (var i in result)
{
Console.WriteLine(i);
}
}
Here is a DEMO.
For your second case;
public static void Main(string[] args)
{
List<string> A = new List<string> { "Personal", "Tech" };
List<string> B = new List<string> { "Personal", "Tech", "General"};
var result = B.Except(A);
foreach ( var i in result )
{
Console.WriteLine(i);
}
}
Here is a DEMO.
listA.Except(listB) will give you all of the items in list A that are not in list B.
Than do the reverse.
Use Enumerable.Except
var result = list1.Except(list2).ToList();
var q = A.Intersect(B);//"Personal" , "Tech"
var r = B.Except(A);//"General"

Categories

Resources