C# LINQ : Summarise specific inner class properties from outer class collection - c#

Drawing on Loop Through An Objects Properties In C# and Using LINQ to loop through inner class properties in outer class collection
Where you have objects (Phase) in a collection (PhaseRepo), I believe it is possible to specify propertiesOfInterest in the objects (Phase) and create a Dictionary to summarise the properties.
Please find below my attempt in LinqPad. Please assist with the syntax or advise an alternate approach.
Thank you
enum Dir {Up, Dn}
struct BmkKey
{
public Dir Direction;
public string DetailType;
}
class Phase
{
public Dir Direction { get; set; }
public double StartToTerminationBars { get; set; }
public double StartToTerminationPriceChange { get; set; }
public double StartToTerminationGa { get; set; }
public double NotRequiredProperty { get; set; }
}
class PhaseRepo
{
public List<Phase> Phases { get; private set; }
public List<Phase> GetPhases()
{
return new List<Phase>()
{
new Phase() { Direction = Dir.Up, StartToTerminationBars = 3.0, StartToTerminationPriceChange = 4.0, StartToTerminationGa = 4.0},
new Phase() { Direction = Dir.Up, StartToTerminationBars = 6.0, StartToTerminationPriceChange = 8.0, StartToTerminationGa = 4.0},
new Phase() { Direction = Dir.Dn, StartToTerminationBars = 3.0, StartToTerminationPriceChange = -4.0, StartToTerminationGa = -4.0},
new Phase() { Direction = Dir.Dn, StartToTerminationBars = 6.0, StartToTerminationPriceChange = -8.0, StartToTerminationGa = -4.0},
};
}
}
void Main()
{
var phaseRepo = new PhaseRepo();
var phases = phaseRepo.GetPhases();
//phases.Dump();
var propertiesOfInterest = typeof (Phase).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(prop => prop.Name == "StartToTerminationBars"
|| prop.Name == "StartToTerminationPriceChange"
|| prop.Name == "StartToTerminationGa")
.ToList();
//propertiesOfInterest.Dump();
// Please Help...
var test = propertiesOfInterest
.SelectMany(propertyInfo => phases
.Select(phase => phase)
.Select(keyValuePair => new
{
phase.Direction,
keyValuePair.Key,
keyValuePair.Value
})
.Select(arg => new
{
Key = new BmkKey
{
Direction,
DetailType = propertyInfo.Name
},
Value = (double)propertyInfo.GetValue(arg.Value, null)
}))
.GroupBy(grp => grp.Key)
.ToDictionary(grp => grp.Key, grp => x => x.ToList());
test.Dump();
}
Expected output:

Solution
Replace the part following
//Please Help...
with the following:
var test = propertiesOfInterest
.SelectMany(propertyInfo => phases
.Select(phase => new
{
phase.Direction,
propertyInfo.Name,
Value = (double)propertyInfo.GetValue(phase)
}))
.GroupBy(o => new BmkKey { Direction = o.Direction, DetailType = o.Name })
.ToDictionary(grp => grp.Key, grp => grp.Select(x => x.Value));
This essentially gives the expected output shown in your question. (The order is different; adjust using OrderBy() as your needs require.)
Explanation
There were a few issues in your code, but the primary problems were:
The call to GetValue() invoked on the wrong object
The key selector in the .ToDictionary() call.
In addition, the following are not wrong, but unnecessary:
The .Select(phase => phase) call, which does nothing--like a line of code specifying x = x;
Building the key before grouping. Instead, you can simply define the key during the .GroupBy() operation.

Related

compare 2 properties inside 2 linq lists and return a boolean based on the comparision

I've tried to find the answer to this but nothing seems to fit it quite right.
Requirement: From a known list of FooObjects, return a list of the Foo Id's whose data satisfies all the search criteria.
Here is my code:
class testClass
{
public class SearchItem
{
string Term { get; set; }
decimal Confidence { get; set; }
}
public class FooObject
{
public Guid Id { get; set; }
public List<Data> Data { get; set; }
}
public class Data
{
public string Text { get; set; }
public decimal Confidence { get; set; }
}
[Test]
public void Test()
{
var searchItems = new List<SearchTerm>
{
new SearchTerm{ Confidence = (decimal)1, Term = "TestWord" },
new SearchTerm{ Confidence = (decimal)1, Term = "TestWord2" },
};
var FooObjects = new List<FooObject>
{
new FooObject{Id = new Guid(), Data = new List<Data>
{
new Data{Text = "TestWord", Confidence = 1},
new Data{Text = "TestWord2", Confidence = 1},
new Data{Text = "SomeOtherWord", Confidence = 1},
}
}
};
//result is a list of the Foo IDs
var result = FooObjects.Where(foo => !searchItems.Select(item => item.Term).Except(foo.Data.Select(dt => dt.Text).Distinct()).Any())
.Select(foo => foo.Id).ToList();
Assert.That(result.Count, Is.EqualTo(1));
searchItems.Add(new SearchTerm{Term = "NotFoundString"});
result = FooObjects.Where(foo => !searchItems.Select(item => item.Term).Except(foo.Data.Select(dt => dt.Text).Distinct()).Any())
.Select(foo => foo.Id).ToList();
Assert.That(result.Count, Is.EqualTo(0));
}
}
I now need to modify this so that I can compare against the confidence of each word
Question:
How do I modify the LINQ to compare the confidence and the Term against my data
Instead of matching any criteria like #dymanoid said in his answer, you should be looking to satisfy all the search items/terms (you are mixing these up in your example code, be consistent).
var result = FooObjects
.Where(f => searchItems.All(
s => f.Data.Exists(d => d.Text == s.Term && d.Confidence == s.Confidence)))
.Select(f => f.Id);
Maybe you're looking for something like this:
var result = FooObjects
.Where(foo => foo.Data.Any(d => searchTerms.Any(
si => d.Term == si.Text && d.Confidence == si.Confidence)))
.Select(foo => foo.Id);
Keep in mind that this search isn't effective - if your data sets are large, the performance will be bad.

join two or more list with Lambda but not using the method or type "Join"

I am very new to C#. I have a method in mapper class which accepts two parameters List1 and List2 as source and return a List of Model class which has fields defined. Fields come from both the list types.
Below is the class(SOHMapper)/method(Map)
public class SOHMapper : ISOHMapper
{
public List<StockonHand> Map(List<WarehouseOnHand> warehouseonhands, List<SPR_SKU> SKus)
{
var Stockonhands = warehouseonhands.Join(SKus, x1 => x1.ItemNumber, x2 => x2.ItemId, (x1, x2) => new
{ x1.Quantity, x1.ProductStyleId, x1.ItemNumber, x2.GNumber, x2.Style }).Join(SKus,
x1 => x1.ProductStyleId, x2 => x2.Style, (x1, x2) => new
{ x1.Quantity, x1.ItemNumber, x2.GNumber }).GroupBy(g => new { g.GNumber, g.ItemNumber })
.Select(n => new StockonHand
{
DFUCode = "\"" + n.Key.GNumber + "\"",
ItemNumber = "\"" + n.Key.ItemNumber + "\"",
CompanyCode = "\"" + "VPAC" + "\"",
NoOfCases = n.Sum(s => s.Quantity),
Date = DateTime.Now.ToString("dd/MM/yyyy")
}).ToList();
return Stockonhands;
}
}
Below is my Model Class ( Stockonhand) :
public class StockonHand
{
public string DFUCode { get; set; }
public string ItemNumber { get; set; }
public string CompanyCode { get; set; }
public decimal NoOfCases { get; set; }
public string Date { get; set; }
}
Now this works fine and I get the required data. But the requirement is not to use the "Join" method/keyword in the lambda expression. Is there any other way I can extract columns from both the List ? Is there a way "Where" method/keyword be used to get the fields from two different List ?
In method syntax:
var result = warehouseonhands.SelectMany(w => SKus.Where(s => s.ItemId == w.ItemNumber)
.Select(s => new { w, s }));
In query syntax:
var result = from w in warehouseonhands
from s in SKus
where w.ItemNumber == s.ItemId
select new { w, s };

Return an new object from join using EntityFramework

I am a little bit stuck here.
I got a Database with tables for projects and versions of these projects:
public class Project
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public List<ProjectVersion> Versions { get; set; } = new List<ProjectVersion>();
public Project() { }
}
and
public class ProjectVersion
{
public int Id { get; set; }
public string Version { get; set; }
public string Checksum { get; set; }
public string Description { get; set; }
public ICollection<EntryPoint> EntryPoints { get; set; } = new List<EntryPoint>();
public ICollection<System> Systems { get; set; } = new List<System>();
public ProjectVersion() { }
}
now I want to get a specific version of a project and some detailed information
public static Project GetVersionByProjectId( int projectId, string version )
{
using ( var ctx = new DatabaseContext() )
{
var query =
ctx.Projects
.Where(p => p.Id.Equals(projectId))
.Join(
ctx.Versions.Where( v => v.Version.Equals( version )),
p => p.Id,
v => v.ProjectId,
(p, v) => new Project
{
Name = p.Name,
Type = p.Type,
Id = p.Id,
Versions = new List<ProjectVersion>
{
new ProjectVersion
{
Checksum = v.Checksum,
Description = v.Description,
Version = v.Version ,
EntryPoints = new List<EntryPoint>(v.EntryPoints),
Systems = new List<System>(v.Systems)
}
}
}
)
.Select(x => x);
var result = query.ToList();
return result[0];
}
}
if I remove the whole
Versions = new List<ProjectVersion>
it works and I get the Project but not the Version. When I tried the LINQ in LINQPad I got following error:
Cannot create a query result of type 'System.Collections.Generic.List`1[UserQuery+ProjectVersion]'.
How can I get the Project with the requested Version?
UPDATE
thanks to the ideas of #RomanoZumbé and #Maritim I could solve it. Problem was different classes of the models.
using ( var ctx = new DatabaseContext() )
{
var query =
ctx.Projects
.Include(p => p.Versions)
.Where(p => p.Id.Equals(projectId))
.Select( p =>
new Project()
{
Id = p.Id,
Name = p.Name,
Type = p.Type,
Versions =
p.Versions
.Where( v => v.Version.Equals(version))
.Select( v =>
new ProjectVersion()
{
Checksum = v.Checksum,
Description = v.Description,
EntryPoints =
v.EntryPoints
.Select( e => new EntryPoint()
{
Call = e.Call,
Step = e.Step
})
.ToList()
})
.ToList()
})
.Select(x => x);
var result = query.ToList();
return result[0];
}
I don't know if I understood it correctly, and this is from top of my mind, but I think it would be easier if you used the Include(...) statement. And since you already included the project 'Versions' (which is equivalent to a JOIN statement in the database), you can do something like:
var query =
ctx.Projects
.Include(p => p.Versions)
.Where(p => p.Id.Equals(projectId))
.Select(
new Project
{
Name = p.Name,
Type = p.Type,
Id = p.Id,
Versions = p.Versions.Where(v => v.Version.Equals(version))
});
return query.FirstOrDefault();
I'm not entirely sure wether this is what you want to achieve, but maybe it helps.
var res = (from p in ctx.Projects
let v = ctx.Versions.FirstOrDefault(x => p.Versions.Any(v => v.Id == x.Id))
where p.Id == projectId
select new Project
{
Name = p.Name,
Type = p.Type,
Id = p.Id,
Versions = new List<ProjectVersion>()
{
new ProjectVersion
{
Checksum = v.Checksum,
Description = v.Description,
Version = v.Version ,
EntryPoints = new List<EntryPoint>(v.EntryPoints),
Systems = new List<System>(v.Systems)
}
}
});
return res.FirstOrDefault()

Linq between two list with condition

I have settings class
public class Setting
{
public virtual string Key { get; set; }
public virtual string Value { get; set; }
}
and i have to list
IEnumerable<Setting> A1 => contain {"A","1"}{"B","2"}
IEnumerable<Setting> A2 => contain {"A","1"}{"B","5"}
i want linq statment to chose the element from list A2 that have same key and different value here is {"B","5"}
I have try
A2.Where(x => A1.Any(y => y.Value != x.Value)).ToList();
this give me the two elemnts in A2
can any one help me
thank you
**Edit **
my settings class
public class Setting : Entity<int>
{
public virtual DateTime? ModificationDate { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Key { get; set; }
public virtual string Value { get; set; }
public virtual string ModifiedBy { get; set; }
public virtual string Type { get; set; }
public virtual string ValidateRegex { get; set; }
public virtual bool IsSystem { get; set; }
}
and i have return from mvc IEnumerable<Setting> let it name settings,
then i get from database the original settings IEnumerable<Setting> let it name dbsettings
i want to know the changed value from settings to make update on it
You need to compare the Key as well:
A2.Where(x => A1.Any(y => y.Key == x.Key && y.Value != x.Value)).ToList();
The following sample returns { "B", "5" } as the result:
void Main()
{
var a1 = new List<Setting>(new Setting[] {
new Setting() { Key = "A", Value = "1" },
new Setting() { Key = "B", Value = "2" } });
var a2 = new List<Setting>(new Setting[] {
new Setting() { Key = "A", Value = "1" },
new Setting() { Key = "B", Value = "5" } });
var result = a2.Where(x => a1.Any(y => y.Key == x.Key && y.Value != x.Value)).ToList();
Console.WriteLine(result);
}
As you are comparing strings, you should be aware that == and != respectively always compares case-sensitive. So the keys need to be written in the same way in both lists (and also differences in case will be recognized as relevant differences). You can also use an overload of string.Compare to specify the comparison options in greater detail.
This should do it:
A2.Where(x => A1.Any(y => y.Key == x.Key && y.Value != x.Value))
BTW, your Setting class seems like reinventing the wheel. Dictionary, Tuple and NameValueCollection can all do that for you.
var A1 = new List<Setting>(){new Setting(){Key = "A", Value = "1"}};
var A2 = new List<Setting>() { new Setting() { Key = "A", Value = "2" } };
var a1Keys = A1.Select(x => x.Key).ToList();
var dupKeys = A2.Where(x => a1Keys.Contains(x.Key)).Select(x=>x.Key);
var res = A2.Where(x => dupKeys.Contains(x.Key));
For performance reasons you could use a lookup:
var a1KeyLookup = A1.ToLookup(x => x.Key);
List<Setting> a2List = A2
.Where(a2 => a1KeyLookup[a2.Key].Any(a1 => a1.Value != a2.Value))
.ToList();
Here's your sample data:
IEnumerable<Setting> A1 = new List<Setting> {
new Setting{Key="A", Value="1"},
new Setting{Key="B", Value="2"},
};
IEnumerable<Setting> A2 = new List<Setting> {
new Setting{Key="A", Value="1"},
new Setting{Key="B", Value="5"},
};
var a1KeyLookup = A1.ToLookup(x => x.Key);
List<Setting> a2List = A2
.Where(a2 => a1KeyLookup[a2.Key].Any(a1 => a1.Value != a2.Value))
.ToList();
It returns as expected a list with a single item: Key="B", Value="5"

GroupBy in lambda expressions

from x in myCollection
group x by x.Id into y
select new {
Id = y.Key,
Quantity = y.Sum(x => x.Quantity)
};
How would you write the above as a lambda expression? I'm stuck on the group into part.
Query continuations (select...into and group...into, but not join...into) are equivalent to just splitting up the query expression. So I like to think of your example as:
var tmp = from x in myCollection
group x by x.Id;
var result = from y in tmp
select new {
Id = y.Key,
Quantity = y.Sum(x => x.Quantity)
};
Change those into dot notation:
var tmp = myCollection.GroupBy(x => x.Id);
var result = tmp.Select(y => new {
Id = y.Key,
Quantity = y.Sum(x => x.Quantity)
});
Then you could combine them back:
var tmp = myCollection.GroupBy(x => x.Id)
.Select(y => new {
Id = y.Key,
Quantity = y.Sum(x => x.Quantity)
});
Once you work out what the C# compiler does with query expressions, the rest is relatively straightforward :)
myCollection
.GroupBy(x => x.Id)
.Select(x =>
new
{
Id = x.Key,
Quantity = x.Sum(y => x.Quantity
});
myCollection.GroupBy(x => x.Id)
.Select(y => new {
Id = y.Key,
Quantity = y.Sum(x => x.Quantity)
});
var mostFrequent =
lstIn.Where(i => !string.IsNullOrEmpty(i))
.GroupBy(s => s)
.OrderByDescending(g => g.Count())
.Select(s => s.Key)
.FirstOrDefault();
So, for most of the answers here, everyone seems to be dealing with getting a simple object of Id made from count of the group, and the Key itself which is group.Key.
Although thats probably the main useage of this. Didn't really satisfy my needs.
For my own case, I basically wanted to group by some object property, then fetch a specific object from that group. Here's a sample code.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var response = new List<ResponseClass>();
var listOfStudents = new List<Student>();
// Insert some objects into listOfStudents object.
listOfStudents.GroupBy(g => g.Class).ToList()
.ForEach(g => response.Add(g.OrderByDescending(s => s.CreatedOn).Select(a =>
new ResponseClass
{
SName = a.StudentName,
SAge = a.Age,
SClass = a.Class,
SCreatedOn = a.CreatedOn,
RandomProperty = Guid.NewGuid().ToString()
})
.First()));
Console.WriteLine("This compiles and should work just fine");
}
class Student
{
public string StudentName { get; set; }
public int Age { get; set; }
public string Class { get; set; }
public DateTime CreatedOn { get; set; }
}
class ResponseClass
{
public string SName { get; set; }
public int SAge { get; set; }
public string SClass { get; set; }
public DateTime SCreatedOn { get; set; }
public string RandomProperty { get; set; }
}
}
If you would rather use a foreach loop (I prefer lambda as I find it easier to read), but if you do, you could do it like so.
foreach (IGrouping<string, Student> groupedStudents in listOfStudents.GroupBy(g => g.Class))
{
response.Add(groupedStudents.OrderByDescending(x => x.CreatedOn).Select(a =>
new ResponseClass
{
SName = a.StudentName,
SAge = a.Age,
SClass = a.Class,
SCreatedOn = a.CreatedOn,
RandomProperty = Guid.NewGuid().ToString()
}).First());
}
Hope this helps someone. :)

Categories