Filter Table data using Linq - linq

I want filter the messages with type X from table below whose TO value is not equal to FROM value of any Type Y records in same table. (eg. ee) using Linq.
Message TO FROM Type
------- ---- ----- ------
aa 11 22 X
bb 33 44 X
cc 55 11 Y
dd 66 33 Y
ee 77 88 X
I have used this but not working
var messages1 = messages.Where(x => x.Type == 'X');
var messages2 = messages.Where(x => x.Type == 'Y');
var filteredMessages = messages1
.Where(x => !messages2.Any(y => y.From == x.To));

I am not able to reproduce your issue, although I am getting the correct output.
private static void Main(string[] args)
{
List<MessageItem> messages = new List<MessageItem>
{
new MessageItem
{
Message = "aa",
From = 22,
To = 11,
Type = "X"
},
new MessageItem
{
Message = "bb",
From = 44,
To = 33,
Type = "X"
},
new MessageItem
{
Message = "cc",
From = 11,
To = 55,
Type = "Y"
},
new MessageItem
{
Message = "dd",
From = 33,
To = 66,
Type = "Y"
},
new MessageItem
{
Message = "ee",
From = 88,
To = 77,
Type = "X"
}
};
var messages1 = messages.Where(x => x.Type == "X").ToList();
var messages2 = messages.Where(x => x.Type == "Y").ToList();
var filteredMessages = messages1.FindAll(x => !messages2.Any(y => x.To == y.From));
}
The model class:
public class MessageItem
{
public string Message { get; set; }
public int From { get; set; }
public int To { get; set; }
public string Type { get; set; }
}
Outputting:

Related

How to group according to a field value

I got result after executing linq query in mvc like below:
[0] = { albumid = 176, selecttionid = 243, orderid = 57 }
[1] = { albumid = 177, selecttionid = 243, orderid = 57 }
[2] = { albumid = 178, selecttionid = 243, orderid = 57 }
[3] = { albumid = 19, selecttionid = 321, orderid = 137 }
......
But I need to create folder for each different selecttionid .How can I do this?
If you just need to create a folder for each diferente selecttionid, than you just need use Select with Distinct, like that:
var selections = mylist.Select(x => x.selecttionid).Distinct();
foreach(var selection in selections)
{
//Code that create a folder for the selectionId
}
If you need the values from the list, than you can use GroupBy.
var groupedSelections = mylist.GroupBy(x => x.selecttionid);
foreach(var groupSelecion in groupedSelections)
{
//Code that create a folder for the groupSelecion.Key
}

get sum from list of objects in linq C#

I have list of objects as described below:
List<Maths> mObjs = new List<Maths>();
mObjs.Add(new Maths{ Name = "Jack", M1 = 10, M2 = 5, M3 = 0, M4 = 2, M5 =1 });
mObjs.Add(new Maths { Name = "Jill", M1 = 2, M2 = 3, M3 = 4, M4 = 1, M5 = 0 });
mObjs.Add(new Maths { Name = "Michel", M1 = 12, M2 = 15, M3 = 10, M4 = 12, M5 = 11 });
Now I need to calculated the total aggregated value for all three people.
I need to get the below results, probably a new other class
List<Results> mRes = new List<Results>();
public class Results{
public string Name { get; set; }
public int TotalValue { get; set; }
}
mRes.Name = "M1"
mRes.TotalValue = 24;
mRes.Name = "M2"
mRes.TotalValue = 23;
mRes.Name = "M3"
mRes.TotalValue = 14;
mRes.Name = "M4"
mRes.TotalValue = 15;
mRes.Name = "M5"
mRes.TotalValue = 12;
How can I get this data from mObjs using linq query? I know we can do it using for, but want to know if there are any better ways to get this using linq query because that reduces lines of code and I have similar requirements in many other places and dont want to write number of foreach or fors every time.
You can use a pre selection list to list both the name and the field to select
var lookups = new Dictionary<string,Func<Maths,int>> {
{"M1", x => x.M1 },
{"M2", x => x.M2 },
{"M3", x => x.M3 },
{"M4", x => x.M4 },
{"M5", x => x.M5 },
};
Then you can simply do
var mRes = dlookups.Select(x => new Results {
Name= x.Key,
TotalValue = mObjs.Sum(x.Value)
}).ToList();
BEGIN UPDATED*
In response to comments
The lambda expression is just a function from your source class to an int.
For example
class Sub1 {
string M3 {get;set;}
int M4 {get;set;}
}
class Math2 {
string Name {get;set;}
string M1 {get;set;}
string M2 {get;set;}
Sub1 Sub {get;set;}
}
var lookups = new Dictionary<string,Func<Math2,int>> {
{ "M1", x => int.Parse(x.M1) },
{ "M2", x => int.Parse(x.M2) },
{ "M3", x => int.Parse(x.Sub.M3) },
{ "M4", x => int.Parse(x.Sub.M4} }
};
Or if you want to put a little error checking in, you can either use functions or embed the code.
int GetInt(string source) {
if (source == null) return 0;
int result;
return int.TryParse(source, out result) ? result : 0;
}
var lookups = new Dictionary<string,Func<Math2,int>> {
{ "M1", x => {
int result;
return x == null ? 0 : (int.TryParse(x,out result) ? result : 0);
},
{ "M2", x => GetInt(x) },
{ "M3", x => x.Sub == null ? 0 : GetInt(x.Sub.M3) },
{ "M4", x => x.Sub == null ? 0 : x.Sub.M4}
};
END UPDATED
If you want to go further you could use reflection to build the lookups dictionary.
Here is a helper function that will generate the lookups for all Integer properties of a class.
public Dictionary<string,Func<T,int>> GenerateLookups<T>() where T: class {
// This just looks for int properties, you could add your own filter
var properties = typeof(T).GetProperties().Where(pi => pi.PropertyType == typeof(int));
var parameter = Expression.Parameter(typeof(T));
return properties.Select(x => new {
Key = x.Name,
Value = Expression.Lambda<Func<T,int>>(Expression.Property(parameter,x),parameter).Compile()
}).ToDictionary (x => x.Key, x => x.Value);
}
Now you can just do:
var mRes=GenerateLookups<Maths>().Select( x => new Results
{
Name = x.Key,
TotalValue = mObjs.Sum(x.Value)
}).ToList();
Not very smart but efficient and readable:
int m1Total= 0;
int m2Total= 0;
int m3Total= 0;
int m4Total= 0;
int m5Total= 0;
foreach(Maths m in mObjs)
{
m1Total += m.M1;
m2Total += m.M2;
m3Total += m.M3;
m4Total += m.M4;
m5Total += m.M5;
}
List<Results> mRes = new List<Results>
{
new Results{ Name = "M1", TotalValue = m1Total },
new Results{ Name = "M2", TotalValue = m2Total },
new Results{ Name = "M3", TotalValue = m3Total },
new Results{ Name = "M4", TotalValue = m4Total },
new Results{ Name = "M5", TotalValue = m5Total },
};
Result:
Name: "M1" TotalValue: 24
Name: "M2" TotalValue: 23
Name: "M3" TotalValue: 14
Name: "M4" TotalValue: 15
Name: "M5" TotalValue: 12
Edit: since you've explicitly asked for LINQ, if the properties are always these five i don't see why you need to use LINQ at all. If the number can change i would use a different structure.
You could for example use
a single List<Measurement> instead of multiple properties where Measurement is another class that stores the name and the value or you could use
a Dictionary<string, int> for efficient lookup.
You can try out some thing like this :
mRes.Add(new Results() { Name = "M1", TotalValue = mObjs.Sum(x => x.M1) });
To programmatically iterate through all the class properties, you might need to employ reflection.

PIVOT with LINQ from Datatable [duplicate]

I have a collection of items that contain an Enum (TypeCode) and a User object, and I need to flatten it out to show in a grid. It's hard to explain, so let me show a quick example.
Collection has items like so:
TypeCode | User
---------------
1 | Don Smith
1 | Mike Jones
1 | James Ray
2 | Tom Rizzo
2 | Alex Homes
3 | Andy Bates
I need the output to be:
1 | 2 | 3
Don Smith | Tom Rizzo | Andy Bates
Mike Jones | Alex Homes |
James Ray | |
I've tried doing this using foreach, but I can't do it that way because I'd be inserting new items to the collection in the foreach, causing an error.
Can this be done in Linq in a cleaner fashion?
I'm not saying it is a great way to pivot - but it is a pivot...
// sample data
var data = new[] {
new { Foo = 1, Bar = "Don Smith"},
new { Foo = 1, Bar = "Mike Jones"},
new { Foo = 1, Bar = "James Ray"},
new { Foo = 2, Bar = "Tom Rizzo"},
new { Foo = 2, Bar = "Alex Homes"},
new { Foo = 3, Bar = "Andy Bates"},
};
// group into columns, and select the rows per column
var grps = from d in data
group d by d.Foo
into grp
select new {
Foo = grp.Key,
Bars = grp.Select(d2 => d2.Bar).ToArray()
};
// find the total number of (data) rows
int rows = grps.Max(grp => grp.Bars.Length);
// output columns
foreach (var grp in grps) {
Console.Write(grp.Foo + "\t");
}
Console.WriteLine();
// output data
for (int i = 0; i < rows; i++) {
foreach (var grp in grps) {
Console.Write((i < grp.Bars.Length ? grp.Bars[i] : null) + "\t");
}
Console.WriteLine();
}
Marc's answer gives sparse matrix that can't be pumped into Grid directly.
I tried to expand the code from the link provided by Vasu as below:
public static Dictionary<TKey1, Dictionary<TKey2, TValue>> Pivot3<TSource, TKey1, TKey2, TValue>(
this IEnumerable<TSource> source
, Func<TSource, TKey1> key1Selector
, Func<TSource, TKey2> key2Selector
, Func<IEnumerable<TSource>, TValue> aggregate)
{
return source.GroupBy(key1Selector).Select(
x => new
{
X = x.Key,
Y = source.GroupBy(key2Selector).Select(
z => new
{
Z = z.Key,
V = aggregate(from item in source
where key1Selector(item).Equals(x.Key)
&& key2Selector(item).Equals(z.Key)
select item
)
}
).ToDictionary(e => e.Z, o => o.V)
}
).ToDictionary(e => e.X, o => o.Y);
}
internal class Employee
{
public string Name { get; set; }
public string Department { get; set; }
public string Function { get; set; }
public decimal Salary { get; set; }
}
public void TestLinqExtenions()
{
var l = new List<Employee>() {
new Employee() { Name = "Fons", Department = "R&D", Function = "Trainer", Salary = 2000 },
new Employee() { Name = "Jim", Department = "R&D", Function = "Trainer", Salary = 3000 },
new Employee() { Name = "Ellen", Department = "Dev", Function = "Developer", Salary = 4000 },
new Employee() { Name = "Mike", Department = "Dev", Function = "Consultant", Salary = 5000 },
new Employee() { Name = "Jack", Department = "R&D", Function = "Developer", Salary = 6000 },
new Employee() { Name = "Demy", Department = "Dev", Function = "Consultant", Salary = 2000 }};
var result5 = l.Pivot3(emp => emp.Department, emp2 => emp2.Function, lst => lst.Sum(emp => emp.Salary));
var result6 = l.Pivot3(emp => emp.Function, emp2 => emp2.Department, lst => lst.Count());
}
* can't say anything about the performance though.
You can use Linq's .ToLookup to group in the manner you are looking for.
var lookup = data.ToLookup(d => d.TypeCode, d => d.User);
Then it's a matter of putting it into a form that your consumer can make sense of. For instance:
//Warning: untested code
var enumerators = lookup.Select(g => g.GetEnumerator()).ToList();
int columns = enumerators.Count;
while(columns > 0)
{
for(int i = 0; i < enumerators.Count; ++i)
{
var enumerator = enumerators[i];
if(enumator == null) continue;
if(!enumerator.MoveNext())
{
--columns;
enumerators[i] = null;
}
}
yield return enumerators.Select(e => (e != null) ? e.Current : null);
}
Put that in an IEnumerable<> method and it will (probably) return a collection (rows) of collections (column) of User where a null is put in a column that has no data.
I guess this is similar to Marc's answer, but I'll post it since I spent some time working on it. The results are separated by " | " as in your example. It also uses the IGrouping<int, string> type returned from the LINQ query when using a group by instead of constructing a new anonymous type. This is tested, working code.
var Items = new[] {
new { TypeCode = 1, UserName = "Don Smith"},
new { TypeCode = 1, UserName = "Mike Jones"},
new { TypeCode = 1, UserName = "James Ray"},
new { TypeCode = 2, UserName = "Tom Rizzo"},
new { TypeCode = 2, UserName = "Alex Homes"},
new { TypeCode = 3, UserName = "Andy Bates"}
};
var Columns = from i in Items
group i.UserName by i.TypeCode;
Dictionary<int, List<string>> Rows = new Dictionary<int, List<string>>();
int RowCount = Columns.Max(g => g.Count());
for (int i = 0; i <= RowCount; i++) // Row 0 is the header row.
{
Rows.Add(i, new List<string>());
}
int RowIndex;
foreach (IGrouping<int, string> c in Columns)
{
Rows[0].Add(c.Key.ToString());
RowIndex = 1;
foreach (string user in c)
{
Rows[RowIndex].Add(user);
RowIndex++;
}
for (int r = RowIndex; r <= Columns.Count(); r++)
{
Rows[r].Add(string.Empty);
}
}
foreach (List<string> row in Rows.Values)
{
Console.WriteLine(row.Aggregate((current, next) => current + " | " + next));
}
Console.ReadLine();
I also tested it with this input:
var Items = new[] {
new { TypeCode = 1, UserName = "Don Smith"},
new { TypeCode = 3, UserName = "Mike Jones"},
new { TypeCode = 3, UserName = "James Ray"},
new { TypeCode = 2, UserName = "Tom Rizzo"},
new { TypeCode = 2, UserName = "Alex Homes"},
new { TypeCode = 3, UserName = "Andy Bates"}
};
Which produced the following results showing that the first column doesn't need to contain the longest list. You could use OrderBy to get the columns ordered by TypeCode if needed.
1 | 3 | 2
Don Smith | Mike Jones | Tom Rizzo
| James Ray | Alex Homes
| Andy Bates |
#Sanjaya.Tio I was intrigued by your answer and created this adaptation which minimizes keySelector execution. (untested)
public static Dictionary<TKey1, Dictionary<TKey2, TValue>> Pivot3<TSource, TKey1, TKey2, TValue>(
this IEnumerable<TSource> source
, Func<TSource, TKey1> key1Selector
, Func<TSource, TKey2> key2Selector
, Func<IEnumerable<TSource>, TValue> aggregate)
{
var lookup = source.ToLookup(x => new {Key1 = key1Selector(x), Key2 = key2Selector(x)});
List<TKey1> key1s = lookup.Select(g => g.Key.Key1).Distinct().ToList();
List<TKey2> key2s = lookup.Select(g => g.Key.Key2).Distinct().ToList();
var resultQuery =
from key1 in key1s
from key2 in key2s
let lookupKey = new {Key1 = key1, Key2 = key2}
let g = lookup[lookupKey]
let resultValue = g.Any() ? aggregate(g) : default(TValue)
select new {Key1 = key1, Key2 = key2, ResultValue = resultValue};
Dictionary<TKey1, Dictionary<TKey2, TValue>> result = new Dictionary<TKey1, Dictionary<TKey2, TValue>>();
foreach(var resultItem in resultQuery)
{
TKey1 key1 = resultItem.Key1;
TKey2 key2 = resultItem.Key2;
TValue resultValue = resultItem.ResultValue;
if (!result.ContainsKey(key1))
{
result[key1] = new Dictionary<TKey2, TValue>();
}
var subDictionary = result[key1];
subDictionary[key2] = resultValue;
}
return result;
}

Linq Convert to Custom Dictionary?

.NET 4, I have
public class Humi
{
public int huKey { get; set; }
public string huVal { get; set; }
}
And in another class is this code in a method:
IEnumerable<Humi> someHumi = new List<Humi>(); //This is actually ISingleResult that comes from a LinqToSql-fronted sproc but I don't think is relevant for my question
var humia = new Humi { huKey = 1 , huVal = "a"};
var humib = new Humi { huKey = 1 , huVal = "b" };
var humic = new Humi { huKey = 2 , huVal = "c" };
var humid = new Humi { huKey = 2 , huVal = "d" };
I want to create a single IDictionary <int,string[]>
with key 1 containing ["a","b"] and key 2 containing ["c","d"]
Can anyone point out a decent way to to that conversion with Linq?
Thanks.
var myDict = someHumi
.GroupBy(h => h.huKey)
.ToDictionary(
g => g.Key,
g => g.ToArray())
Create an IEnumerable<IGrouping<int, Humi>> and then project that into a dictionary. Note .ToDictionary returns a Dictionary, not an IDictionary.
You can use ToLookup() which allows each key to hold multiple values, exactly your scenario (note that each key would hold an IEnumerable<string> of values though not an array):
var myLookup = someHumi.ToLookup(x => x.huKey, x => x.huVal);
foreach (var item in myLookup)
{
Console.WriteLine("{0} contains: {1}", item.Key, string.Join(",", item));
}
Output:
1 contains: a,b
2 contains: c,d

linq delegate function checking from objects

I am trying to find the list of objects which can be replaced.
Class Letter{
int ID;
string Name;
string AdvCode;
int isDeleted;
}
Class Replacers{
int ID;
string MainAdvCode;
string ReplacesAdvCode;
}
example data:
Replacers
0 455 400
1 955 400
2 955 455
LettersA
0 pack 455
1 addon 400
LettersB
0 big 955
1 pack 455
LettersC
0 addon 400
1 big 955
2 pack 455
LettersD
0 pack 455
1 none 019
solution:
LetterA 1 isDeleted
LeterB 1 isDeleted
LetterC 0 and 2 isDeleted
LetterD ---
such that if a Letter has and Advcode of 455 and another has a code of 400 the 400 gets marked for deletion. And then if another Letter has a 955 then the 455 gets marked for deletion and the 400 (which is already marked) is marked for deletion.
The problem is with my current code the 400 and 455 is marking itself for deletion?!?!?
Public class Main{
List<Letter> Letters;
List<Replacers> replaces;
//select the ones to replace the replacements aka the little guys
//check if the replacements replacer exists if yes mark deleted
var filterMethodReplacements = new Func<Letter, bool>(IsAdvInReplacements);//Working
var filterMethodReplacers = new Func<Letter, bool>(IsAdvInReplacers);//NOT WORKING????
var resReplacements=letters.Where(filterMethodReplacements);//working
foreach (Letter letter in resReplacements)
{
//select the Replacers aka the ones that contain the little guys
var resReplacers = letters.Where(filterMethodReplacers);
if (resReplacers != null)
letter.isDeleted = 1;
}
private bool IsAdvInReplacements(Letter letter)
{
return (from a in Replacables where a.ReplaceAdvCode == letter.AdvCode select a).Any();
}
private bool IsAdvInReplacers(Letter letter)
{
//??????????????????????????????
return (from a in Replacables where a.MainAdvCode == letter.AdvCode select a).Any();
}
}
See below, solution was to group by name and then iterate over the replacers.
public class Letter
{
public int ID;
public string Name;
public string AdvCode;
public string Type;
public Boolean isDeleted;
public override string ToString()
{
return this.Name + "[" + this.ID + "]" + ":" + this.Type + " AdvCode=" + AdvCode + " Deleted: " + this.isDeleted.ToString();
}
}
public class Replacers
{
public int ID;
public string MainAdvCode;
public string ReplacesAdvCode;
}
class Program
{
static void Main(string[] args)
{
List<Letter> letters = GetLetters();
List<Replacers> replacables = GetReplacers();
foreach (IGrouping<string, Letter> g in letters.GroupBy(x => x.Name))
{
List<Letter> byName = g.ToList();
foreach (Replacers r in replacables)
{
if (byName.Any(x => x.AdvCode == r.MainAdvCode) && byName.Any(x=>x.AdvCode==r.ReplacesAdvCode))
{
//If we contain the code to replace...
foreach (Letter letter in byName.Where(x=>x.AdvCode==r.ReplacesAdvCode)){
letter.isDeleted = true;
}
}
}
}
Console.WriteLine("Result");
foreach (Letter l in letters.Where(x=>x.isDeleted))
{
Console.WriteLine(l.ToString());
}
Console.WriteLine("Press key to close");
Console.ReadKey();
}
public static List<Letter> GetLetters()
{
List<Letter> letters = new List<Letter>(){
new Letter(){
Name = "LettersA",
ID = 0,
AdvCode="455",
Type="pack",
isDeleted = false
},
new Letter(){
Name = "LettersA",
Type="addon",
ID = 1,
AdvCode="400",
isDeleted = false
},
new Letter(){
Name = "LettersB",
ID = 0,
AdvCode="955",
Type="big",
isDeleted = false
},
new Letter(){
Name = "LettersB",
Type="pack",
ID = 1,
AdvCode="455",
isDeleted = false
},
new Letter(){
Name = "LettersC",
ID = 0,
AdvCode="400",
Type="addon",
isDeleted = false
},
new Letter(){
Name = "LettersC",
Type="big",
ID = 1,
AdvCode="955",
isDeleted = false
},
new Letter(){
Name = "LettersC",
Type="pack",
ID = 2,
AdvCode="455",
isDeleted = false
},
new Letter(){
Name = "LettersD",
ID = 0,
AdvCode="455",
Type="pack",
isDeleted = false
},
new Letter(){
Name = "LettersD",
Type="none",
ID = 1,
AdvCode="019",
isDeleted = false
},
};
return letters;
}
public static List<Replacers> GetReplacers()
{
return new List<Replacers>(){
new Replacers(){
ID = 0,
MainAdvCode = "455",
ReplacesAdvCode = "400"
},
new Replacers(){
ID = 1,
MainAdvCode = "955",
ReplacesAdvCode = "400"
},
new Replacers(){
ID = 2,
MainAdvCode = "955",
ReplacesAdvCode = "455"
},
};
}

Resources