Finding Unique Objects between collections - c#

I have a test where I have two lists created. One represents data that I have gone and collected from a source (of which I have no control over) and the other representing a known data already in my repository.
They look like this:
var newAccounts = new[]
{
new Account
{
Id = 1,
SA_ID = 1,
SA_Name = "Sa_Name1",
RelationshipType = "new",
LE_ID = 1,
LE_GroupID = 1,
SiteID = 1,
MinDate = DateTime.Now.AddDays(-1),
MaxDate = DateTime.Now.AddDays(1),
DaysOn = 1,
Child1 = new List<Child1>{ new Child1
{
SiteID = 1,
MaxDate = DateTime.Today.AddDays(7),
MinDate = DateTime.Today.AddDays(1),
}
},
Child2 = new List<Child2>
{
new Child2
{
SA_ID = 1,
LastUpdate = DateTime.Now.AddDays(-1),
CommentText = "Account added",
Status = AccountStatus.AccountAdded.ToString(),
}
}
},
new Account
{
Id = 2,
SA_ID = 2,
SA_Name = "Sa_Name2",
RelationshipType = "new",
LE_ID = 2,
LE_GroupID = 2,
SiteID = 2,
MinDate = DateTime.Now.AddDays(-2),
MaxDate = DateTime.Now.AddDays(2),
DaysOn = 2,
},
new Account
{
Id = 3,
SA_ID = 3,
SA_Name = "Sa_Name3",
RelationshipType = "new",
LE_ID = 3,
LE_GroupID = 3,
SiteID = 3,
MinDate = DateTime.Now.AddDays(-3),
MaxDate = DateTime.Now.AddDays(3),
DaysOn = 3,
}
};
var knownAccounts = new[]
{
new Account
{
Id = 1,
SA_ID = 1,
SA_Name = "Sa_Name1",
RelationshipType = "new",
LE_ID = 1,
LE_GroupID = 1,
SiteID = 1,
MinDate = DateTime.Now.AddDays(-1),
MaxDate = DateTime.Now.AddDays(1),
DaysOn = 1,
Child1 = new List<Child1>{ new Child1
{
SiteID = 1,
MaxDate = DateTime.Today.AddDays(7),
MinDate = DateTime.Today.AddDays(1),
}
},
Child2 = new List<Child2>
{
new Child2
{
SA_ID = 1,
LastUpdate = DateTime.Now.AddDays(-1),
CommentText = "Account added",
Status = AccountStatus.AccountAdded.ToString(),
}
}
}
};
In my unit tests I am wanting to strip out Account ID 1 from newAccounts so I'm only left with 2 entries in my collection. These are my attempts thus far:
public List<T> ReturnUniqueEntriesList<T>(List<T> newAccounts, List<T> knownAccounts)
{
var a = knownAccounts.Intersect(newAccounts).ToList();
var listA = newAccounts.Except(knownAccounts).ToList();
var listB = knownAccounts.Except(newAccounts).ToList();
var result = listB.Intersect(listA).ToList();
return result;
}
When I run this the final result is 0. a also returns 0 and listA & listB simply return their respective objects.
What is it that I'm doing wrong / missing here? Any help would be appreciated

Override Equals and GetHashcode for Account so that they don't rely on the default implementations (memory address of the object). This means the C# will be able to equate them properly when executing an Except.
For example:
public class Account{
public override bool Equals(object other){
return other is Account a && a.Id == this.Id; //nb; is returns false if other is a null, even if it is an Account
}
public override int GetHashCode(){
return Id.GetHashCode();
}
}
As it is, the following two accounts are very different:
var a = new Account { Id = 1 };
var b = new Account { Id = 1 };
..because they live at different memory addresses.
By overriding Equals so that it compares the Id of the other, regardless the other properties, you can then essentially realize the situation you seem to describe of "two account objects with the same ID are equivalent"
If other properties factor into the decision, add those too. Hashcode.Combine is a useful method to combine several hashcodes for the puzzle of getting multiple properties' hashcodes to produce a suitable new signle hashcode - https://learn.microsoft.com/en-us/dotnet/api/system.hashcode.combine?view=net-5.0

Related

GroupBy bool and Select values for new list

I am trying to get into more complex Linq queries and right away catch a point I am feeling stuck. I have a following list in DB:
ID ELAPSED TIME APPISRUNNING
1 12 TRUE
2 54 TRUE
3 32 FALSE
Where ELAPSED TIME is TimeSpan and APPISRUNNING is a bool.
I would like to build a chart based on these values (https://github.com/beto-rodriguez/LiveCharts2). Chart build fine with this:
Title = "Analytics";
this.ActivityChartSeries = new ISeries[]
{
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
};
Now I somehow need to first GroupBy bool and then select a new List? I have tried following:
IEnumerable<DataRecord> dataRecords = await this.DataStore.GetItemsAsync();
this.ActivityChartSeries = dataRecords
.GroupBy(g => g.AppIsRunning)
.Select(m => new
{ // BELOW IS TOTALLY UNCLEAR FOR ME
Values = m.Select(r => r.EngineElapsed.Ticks),
Name = m.Select(r => r.Name),
})
.Select(x =>
new PieSeries<double>
{
Values = new List<double> { x.Values.FirstOrDefault() },
Name = x.Name.FirstOrDefault(),
});
Type of assigned variable:
public IEnumerable<ISeries> ActivityChartSeries
This part is totally unclear for me:
Values = m.Select(r => r.EngineElapsed.Ticks),
Name = m.Select(r => r.Name),
How after GroupBy I can create two types of data? Basically I need
"Application Running" and "Values"
"Application is not Running" and "Values"
EDIT:
Code provided by Somar Zein compiles fine:
var results = activityChartSeries
.GroupBy(a=> a.AppIsRunning)
.Select(item=> new PieSeries<double>{
Name = item.Key ? "Application is Running" : "Application is not Running",
Values = item.Select(x=> Convert.ToDouble(x.ElapsedTime.Ticks)).ToList()
});
However as a result I am getting something like this, why it is reloading in a loop?
Here is result:
enter image description here
EDIT2:
So I have created an example for testing purposes:
Class:
public class DataModel
{
public int Id { get; set; }
public TimeSpan ElapsedTime { get; set; }
public bool AppIsRunning { get; set; }
}
Code:
List<DataModel> records = new List<DataModel>();
records.Add(new DataModel { Id = 1, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 2, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 3, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 4, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 5, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
this.ActivityChartSeries = records
.GroupBy(g => g.AppIsRunning)
.Select(item => new PieSeries<double>
{
Name = item.Key ? "Running" : "Not Running",
Values = new double[] { 2, 4 },
});
I get the same reloading effect, even thou originally provided Example from LiveCharts work fine.
you could try doing something like following:
var results = activityChartSeries
.GroupBy(a=> a.AppIsRunning)
.Select(item=> new PieSeries<double>{
Name = item.Key ? "Application is Running" : "Application is not Running",
Values = item.Select(x=> Convert.ToDouble(x.ElapsedTime.Ticks)).ToList()
});
hope that could be helpful!

Use LINQ to compare two lists, and produce a third one with results from one OR the other

I have two lists of elements that differ only on the IsTemporalPaymentTerm boolean field.
I want to use LINQ to compare the two lists, entry by entry, and produce a third list that has, for each entry, the one where IsTemporalPaymentTerm = true, and if there isn't one, then I want the one where IsTemporalPaymentTerm = false.
Here's some sample code:
var allResults = db.PaymentTerms
.AsQueryable()
.Where(y => y.WorkDate == date
&& y.ProjectID == ProjectID
&& y.CompanyID == CompanyID
&& y.PayeeID == PayeeID);
//TABLE WITH ONLY TEMPORAL PAYMENT TERMS
var onlyTemporalResults = allResults.Where(x => x.IsTemporalPaymentTerm);
//TABLE WITH ONLY NON-TEMPORAL PAYMENT TERMS
var nonTemporalResults = allResults.Where(x => !x.IsTemporalPaymentTerm);
So, basically what I want is to compare onlyTemporalResults against nonTemporalResults , and get a final list that has either the temporal payment term, OR the non-temporal payment term if no temporal payment term could be found.
Pseudo code example:
List<PaymentTerms> TemporalPaymentTerms = new List<PaymentTerms>();
PaymentTerm unnaprovedPT1 = new PaymentTerm { PayeeID = 1, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false };
PaymentTerm unnaprovedPT2 = new PaymentTerm { PayeeID = 2, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false };
TemporalPaymentTerms.Add(unnaprovedPT1);
TemporalPaymentTerms.Add(unnaprovedPT2);
List<PaymentTerms> NonTemporalPaymentTerms = new List<PaymentTerms>();
PaymentTerm approvedPT1 = new PaymentTerm { PayeeID = 2, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = true};
PaymentTerm approvedPT1 = new PaymentTerm { PayeeID = 3, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = true};
//LINQ query that merges both lists goes here.
//FINAL EXPECTED RESULT:
List<PaymentTerms> FinalList = [
{PayeeID = 1, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false},
{PayeeID = 2, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false},
{PayeeID = 3, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = true}
];
I know this can be done iterating over the two lists (temporal and non-temporal Payment Terms), and then comparing them, but I guess my question is:
Can this be done, more efficiently and in a more elegant way, using a single LINQ query? Maybe a certain form of join that I am missing? Conditional Where clauses?
So far I have failed to see the answer. Thanks in advance!
Is this what you're looking for?
static void Main(string[] args)
{
RunPaymentTermsTest();
Console.WriteLine("Done!");
Console.ReadLine();
}
private static void RunPaymentTermsTest()
{
var temporalPaymentTerms = new List<PaymentTerm>
{
new PaymentTerm { PayeeID = 1, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false },
new PaymentTerm { PayeeID = 2, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false }
};
var nonTemporalPaymentTerms = new List<PaymentTerm>()
{
new PaymentTerm { PayeeID = 2, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = true },
new PaymentTerm { PayeeID = 3, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = true }
};
var toAdd = temporalPaymentTerms
.Where(x =>
!nonTemporalPaymentTerms.Any(y =>
y.CompanyID == x.CompanyID &&
y.PayeeID == x.PayeeID &&
y.ProjectID == x.ProjectID))
.ToList();
var results = nonTemporalPaymentTerms;
results.AddRange(toAdd);
foreach (var result in results.OrderBy(x => x.PayeeID).ThenBy(x => x.CompanyID).ThenBy(x => x.ProjectID))
{
Console.WriteLine(
$"PayeeID: {result.PayeeID}, CompanyID: {result.CompanyID}, ProjectID: {result.ProjectID}, IsTemporalPaymentTerm: {result.IsTemporalPaymentTerm}");
}
}

Filter hierarchical list retaining parent using LINQ

I have a model class which looks something like this:
public class Employee
{
public int Id {get;set;}
public int ParentId {get;set;}
public string Name{get;set;}
public string Designation {get;set;}
}
using which I simulated a list:
new List<Employee> employees
{
new Employee{Id = 1, ParentId = 0, Name = "A", Designation = "CEO" },
new Employee{Id = 2, ParentId = 1, Name = "B", Designation = "Manager" },
new Employee{Id = 3, ParentId = 1, Name = "C", Designation = "Manager" },
new Employee{Id = 4, ParentId = 2, Name = "D", Designation = "Lead" },
new Employee{Id = 5, ParentId = 3, Name = "E", Designation = "Lead" },
new Employee{Id = 6, ParentId = 4, Name = "F", Designation = "Developer" },
new Employee{Id = 7, ParentId = 4, Name = "G", Designation = "Developer" },
new Employee{Id = 8, ParentId = 5, Name = "H", Designation = "Developer" }
};
Well I need to write a LINQ query to filter the above list so that even the parent objects(if present) are retained during the filtering. I could not quiet wrap my head around the retainment of the parent part where I always end up getting it wrong.
To make it more clear this is what is the expected filtered list in case the filter search criteria are the Ids 6 and 7:
{
new Employee{Id = 1, ParentId = 0, Name = "A", Designation = "CEO" },
new Employee{Id = 2, ParentId = 1, Name = "B", Designation = "Manager" },
new Employee{Id = 4, ParentId = 2, Name = "D", Designation = "Lead" }
new Employee{Id = 6, ParentId = 4, Name = "F", Designation = "Developer" },
new Employee{Id = 7, ParentId = 4, Name = "G", Designation = "Developer" }
}
and if the Id to filter is 8:
{
new Employee{Id = 1, ParentId = 0, Name = "A", Designation = "CEO" },
new Employee{Id = 3, ParentId = 1, Name = "C", Designation = "Manager" },
new Employee{Id = 5, ParentId = 3, Name = "E", Designation = "Lead" },
new Employee{Id = 8, ParentId = 5, Name = "H", Designation = "Developer" }
}
and if the Id to filter is 2:
{
new Employee{Id = 1, ParentId = 0, Name = "A", Designation = "CEO" },
new Employee{Id = 2, ParentId = 1, Name = "B", Designation = "Manager" }
}
You can implement a help method, EmployeeAndBosses which returns given employee and all the parents:
private static IEnumerable<Employee> EmployeeAndBosses(Employee value,
IEnumerable<Employee> collection) {
for (Employee item = value;
item != null;
item = collection.FirstOrDefault(x => x.ParentId == item.Id))
yield return item;
}
then you can filter topmost employee in the hierarchy, and add their bosses then:
HashSet<int> ids = new HashSet<int>() {
6, 7
};
var result = employees
.Where(item => ids.Contains(item.Id)) // topmost
.SelectMany(item => EmployeeAndBosses(item, employees)) // topmost and parents
.GroupBy(item => item.Id) // Duplicates (e.g. CEO) removing
.Select(group => group.First()); //
Edit: If you have a huge collection(s) and that's why FirstOrDefault and GroupBy are bad choice, you can implement Bread First Search:
private static IEnumerable<Employee> MyFilter(IEnumerable<Employee> list,
IEnumerable<int> idsToFind) {
Dictionary<int, Employee> stuff = list
.ToDictionary(item => item.Id, item => item);
HashSet<int> ids = new HashSet<int>(idsToFind);
HashSet<int> completed = new HashSet<int>();
Queue<Employee> agenda = new Queue<Employee>(list.Where(item => ids.Contains(item.Id)));
while (agenda.Count > 0) {
Employee current = agenda.Dequeue();
if (null != current && completed.Add(current.Id)) {
yield return current;
if (stuff.TryGetValue(current.ParentId, out current))
agenda.Enqueue(current);
}
}
}
As some comments seem to be quite... Subjective... Here is a simple (but somewhat inefficient) extension that handles your requirements like a charm:
(assuming you'll never hire an employee as a boss to another employee that in turn is their boss, but such madness would probably break the company quicker than it breaks the query)
public static IEnumerable<Employee> FindByIdsAndIncludeParents(this IEnumerable<Employee> employees, params int[] targetIds)
=> employees
.Where(r => targetIds.Contains(r.Id))
.SelectMany(r => employees.FindByIdsAndIncludeParents(r.ParentId).Append(r))
.Distinct();
As some are not quite as keen of exchanging this quite expensive operation for the mere beauty of it, we could trade in some beauty for speed using a dictionary as entry point for instant access to the appended boss-search:
public static IEnumerable<Employee> FindFriendsFaster(this IEnumerable<Employee> employees, params int[] targetIds)
=> employees
.ToDictionary(e => e.Id, e => e)
.FindWithBossFriend(targetIds)
.Distinct();
public static IEnumerable<Employee> FindWithBossFriend(this IDictionary<int, Employee> hierarchy, params int[] targetIds)
=> targetIds
.Where(eId => hierarchy.ContainsKey(eId))
.Select(eId => hierarchy[eId])
.SelectMany(e => hierarchy.FindWithBossFriend(e.ParentId).Append(e));
As you might be able to spot, I personally can't seem to be able to trade in any more of my dignity for the possible removal of that last .Distinct(), but there are rumors going around some would be.

Is there a way to retrieve a SINGLE RECORD randomly in a List using Shuffle()? - C#

Found this Post and it has good solution when shuffling items in List<T>.
But in my case i have a class Person which is defined as this:
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Position { get; set; }
}
This is my implementation and usage:
List<Person> workers = new List<Person>()
{
new Person { Id = 1, Name = "Emp 1", Position = "Cashier"},
new Person { Id = 2, Name = "Emp 2", Position = "Sales Clerk"},
new Person { Id = 3, Name = "Emp 3", Position = "Cashier"},
new Person { Id = 4, Name = "Emp 4", Position = "Sales Clerk"},
new Person { Id = 5, Name = "Emp 5", Position = "Sales Clerk"},
new Person { Id = 6, Name = "Emp 6", Position = "Cashier"},
new Person { Id = 7, Name = "Emp 7", Position = "Sales Clerk"}
};
Now i want to shuffle all records and get 1 Sales Clerk. Here is my code and is working:
var worker = workers.OrderBy(x => Guid.NewGuid()).Where(x => x.Position == "Sales Clerk").First();
// This can yield 1 of this random result (Emp 2, Emp 4, Emp 5 and Emp 7).
Console.WriteLine(worker.Name);
But according to the given Post GUID is not good for randomizing record. And the worst is i cant use Shuffle() and call the Where and First() extensions to get the desired result.
How can i do that with Shuffle() extension?
If the question is how to get it so you can chain Shuffle() with the rest of your Linq operators, the answer is to modify the Shuffle method to return reference to the list shuffled:
public static IEnumerable<T> Shuffle<T>(this IList<T> list)
{
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
int n = list.Count;
while (n > 1)
{
byte[] box = new byte[1];
do provider.GetBytes(box);
while (!(box[0] < n * (Byte.MaxValue / n)));
int k = (box[0] % n);
n--;
T value = list[k];
list[k] = list[n];
list[n] = value;
}
return list;
}
Your code then becomes:
var worker = workers.Shuffle().Where(x => x.Position == "Sales Clerk").First();
Random oRandom = new Random();
var worker = workers[oRandom.Next(0,workers.Count)];

How to select records with duplicate column by group using LINQ?

For some reason some records have the same ID. Aim is to list all the whole record which have the same ID. For example, how can group the following records by GroupId using LINQ, and find all records with the same ID and list them all? (thus merging all rows in each group into one)
var list = new List<Record>()
{
new Record() { GroupId = 0, ValueA = 20, ValueB = 300 },
new Record() { GroupId = 1, ValueA = 30, ValueB = 700 },
new Record() { GroupId = 1, ValueA = 40, ValueB = 500 },
new Record() { GroupId = 2, ValueA = 80, ValueB = 300 },
new Record() { GroupId = 2, ValueA = 20, ValueB = 200 },
new Record() { GroupId = 2, ValueA = 20, ValueB = 200 }
};
Expect result is the last 5 records.
Another way is:
var results = (from l in list
group new {l.ValueA, l.ValueB} by l.GroupId
into g
select new {GroupId = g.Key, Values = g.ToList()}).ToList();
If you prefer lambda expression, then
var results = (list.GroupBy(l => l.GroupId, l => new {l.ValueA, l.ValueB})
.Select(g => new {GroupId = g.Key, Values = g.ToList()})).ToList();
This should give you records based on unique GroupIdalong the other values associated.
EDIT
foreach (var result in results)
{
foreach (var value in result.Values)
{
int valueA = value.ValueA;
int valueB = value.ValueB;
}
}
I think you are looking for something similar to this.
Hope it helps.
Answer for "how can group the following records by GroupId using LINQ"
var groupList = list.GroupBy((mRecord) => mRecord.GroupId).ToList();
var groups = list.ToLookup(record => record.GroupId);
var groups1 = groups[1]; //all records with GroupId = 1

Categories

Resources