i have a list
List<PossibleSolutionCapacitors> PossibleSolution = new List<PossibleSolutionCapacitors>();
here is its class
class PossibleSolutionCapacitors
{
public int CapacitorALocation { get; set; }
public int CapacitorBLocation { get; set; }
public int CapacitorCLocation { get; set; }
}
i have 3 integers
int A;
int B;
int C;
i need to check if any combination of A,B,C is contained in the list possible solutions
i.e if the following are in the list (Boolean saying true/false is enough)
A,B,C
A,C,B
B,A,C
etc...
is this possible ?
thanks
Damo
A variation on Save's solution:
var fixedSet = new HashSet<int>(){A,B,C};
bool result = PossibleSolutions.Any(x => fixedSet.SetEquals(
new[] { x.CapacitorALocation,x.CapacitorBLocation,x.CapacitorCLocation }));
var query = PossibleSolution.Any(x=>HashSet<int>.CreateSetComparer()
.Equals(new HashSet<int>(){A,B,C}
,new HashSet<int>(){x.CapacitorALocation,x.CapacitorBLocation,x.CapacitorCLocation}));
To save some time, you can create the HashSet<int>(){A,B,C} and the comparer beforehand, and call it in your code, with something like:
var fixedSet = new HashSet<int>(){A,B,C};
IEqualityComparer<HashSet<int>> comparer = HashSet<int>.CreateSetComparer();
var query = PossibleSolution.Any(
x=>comparer.Equals(fixedSet,new HashSet<int>(){x.CapacitorALocation,x.CapacitorBLocation,x.CapacitorCLocation}));
And finally, for a version that uses SetEquals instead of the comparer, check Thomas Levesque solution.
Related
I am having a bit of a frustrating time finding a simple method to compare and prove that the contents of two lists are equal. I have looked at a number of solutions on stackoverflow but I have not been successful. Some of the solutions look like they will require a large amount of work to implement and do something that on the face of it to my mind should be simpler, but perhaps I am too simple to realize that this cannot be done simply :)
I have created a fiddle with some detail that can be viewed here: https://dotnetfiddle.net/cvQr5d
Alternatively please find the full example below, I am having trouble with the object comparison method (variable finalResult) as it's returning false and if the content were being compared I would expect the value to be true:
using System;
using System.Collections.Generic;
using System.Linq;
public class ResponseExample
{
public Guid Id { get; set; } = Guid.Parse("00000000-0000-0000-0000-000000000000");
public int Value { get; set; } = 0;
public string Initials { get; set; } = "J";
public string FirstName { get; set; } = "Joe";
public string Surname { get; set; } = "Blogs";
public string CellPhone { get; set; } = "0923232199";
public bool EmailVerified { get; set; } = false;
public bool CellPhoneVerified { get; set; } = true;
}
public class Program
{
public static void Main()
{
var responseOne = new ResponseExample();
var responseTwo = new ResponseExample();
var responseThree = new ResponseExample();
var responseFour = new ResponseExample();
List<ResponseExample> objectListOne = new List<ResponseExample>();
objectListOne.Add(responseOne);
objectListOne.Add(responseTwo);
List<ResponseExample> objectListTwo = new List<ResponseExample>();
objectListTwo.Add(responseThree);
objectListTwo.Add(responseFour);
bool result = objectListOne.Count == objectListTwo.Count();
Console.WriteLine($"Count: {result}");
bool finalResult = ScrambledEquals<ResponseExample>(objectListOne, objectListTwo);
Console.WriteLine($"Object compare: {finalResult}");
}
//https://stackoverflow.com/a/3670089/3324415
public static bool ScrambledEquals<T>(IEnumerable<T> list1, IEnumerable<T> list2)
{
var cnt = new Dictionary<T,
int>();
foreach (T s in list1)
{
if (cnt.ContainsKey(s))
{
cnt[s]++;
}
else
{
cnt.Add(s, 1);
}
}
foreach (T s in list2)
{
if (cnt.ContainsKey(s))
{
cnt[s]--;
}
else
{
return false;
}
}
return cnt.Values.All(c => c == 0);
}
}
As people in comments have pointed out this will not work as comparing a complex type by default compares whether the reference is the same. Field by field comparison will not work without implementing equality methods (and then you would need to overload GetHashCode and so on). See https://learn.microsoft.com/en-us/dotnet/api/system.object.equals?view=net-5.0
However, if you can use c# 9, which is what you have in the fiddle you can define the type as a record instead of class. Records have built in field by field comparison. See https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/records#characteristics-of-records
So public class ResponseExample would become public record ResponseExample and your code works as you expect.
Use Enumerable.All<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) Method which Determines whether all elements of a sequence satisfy a condition.
Once you have initilized your two List
list1.All(x=>list2.Contains(x))
This works by ensuring that all elements in list2 are containted in list1 otherwise returns false
Your method as is will compare if the 2 lists contain the same objects. So it is returning false as there are 4 different objects. If you create your list like this, using the same objects, it will return true:
List<ResponseExample> objectListOne = new List<ResponseExample>();
objectListOne.Add(responseOne);
objectListOne.Add(responseTwo);
List<ResponseExample> objectListTwo = new List<ResponseExample>();
objectListTwo.Add(responseTwo);
objectListTwo.Add(responseOne);
To get a true value when the contents of the objects are the same you could serialize the objects into a json string like this:
public static bool ScrambledEquals<T>(IEnumerable<T> list1, IEnumerable<T> list2)
{
JavaScriptSerializer json = new JavaScriptSerializer();
var cnt = new Dictionary<string,
int>();
foreach (T _s in list1)
{
string s = json.Serialize(_s);
if (cnt.ContainsKey(s))
{
cnt[s]++;
}
else
{
cnt.Add(s, 1);
}
}
foreach (T _s in list2)
{
string s = json.Serialize(_s);
if (cnt.ContainsKey(s))
{
cnt[s]--;
}
else
{
return false;
}
}
return cnt.Values.All(c => c == 0);
}
If the performance is not a big deal, you can use Newtonsoft.Json. We will be able to compare different types of objects as well as run a deep equals check.
First install the package:
Install-Package Newtonsoft.Json
Here is the code snip:
public static bool DeepEqualsUsingJson<T>(IList<T> l1, IList<T> l2)
{
if (ReferenceEquals(l1, l2))
return true;
if (ReferenceEquals(l2, null))
return false;
if (l1.Count != l2.Count)
return false;
var l1JObject = l1.Select(i => JObject.FromObject(i)).ToList();
var l2JObject = l2.Select(i => JObject.FromObject(i)).ToList();
foreach (var o1 in l1JObject)
{
var index = l2JObject.FindIndex(o2 => JToken.DeepEquals(o1, o2));
if (index == -1)
return false;
l2JObject.RemoveAt(index);
}
return l2JObject.Count == 0;
}
I have a text file that I want to be sorted.
Each line has a package name, a pipe and a version number.
Examples:
AutoFixture|4.15.0
Castle.Windsor.Lifestyles|0.3.0
I tried to use the default list.Sort() method but I obtained:
AutoFixture|4.15.0
Castle.Core|3.3.0
Castle.Windsor.Lifestyles|0.3.0
Castle.Windsor|3.3.0
FluentAssertions|5.10.3
Instead of
AutoFixture|4.15.0
Castle.Core|3.3.0
Castle.Windsor|3.3.0
Castle.Windsor.Lifestyles|0.3.0
FluentAssertions|5.10.3
As shown, I would like "Castle.Windsor" to appear before "Castle.Windsor.Lifestyles".
I'm pretty sure I have to use the IComparer but I can't find a way to get the shorter name first.
So far, I created a custom sort like this which is not working..
public class PackageComparer : IComparer<string>
{
// Assume that each line has the format: name|number
private readonly Regex packageRegEx = new Regex(#"[\w.]+\|[\d.]+", RegexOptions.Compiled);
public int Compare(string x, string y)
{
var firstPackage = this.packageRegEx.Match(x);
var firstLeft = firstPackage.Groups[1].Value;
var firstRight = firstPackage.Groups[2].Value;
var secondPackage = this.packageRegEx.Match(y);
var secondLeft = secondPackage.Groups[1].Value;
var secondRight = secondPackage.Groups[2].Value;
if (firstLeft < secondLeft)
{
return -1;
}
if (firstRight > secondLeft)
{
return 1;
}
return string.CompareOrdinal(firstSceneAlpha, secondSceneAlpha);
}
}
Well, you can use Linq, split by the pipe and order by the package name then by the versioning:
var input = #"AutoFixture|4.15.0
Castle.Core|3.3.0
Castle.Windsor.Lifestyles|0.3.0
Castle.Windsor|3.3.0
FluentAssertions|5.10.3
Castle.Core|3.1.0";
var list = input.Split(new string[]{"\r\n","\n"},StringSplitOptions.None).ToList();
list = list
.OrderBy(x => x.Split('|')[0])
.ThenBy(x => new Version(x.Split('|')[1]))
.ToList();
Outputs:
AutoFixture|4.15.0
Castle.Core|3.1.0
Castle.Core|3.3.0
Castle.Windsor|3.3.0
Castle.Windsor.Lifestyles|0.3.0
FluentAssertions|5.10.3
You can do something like this:
public class YourClassName
{
public string PackageName { get; set; }
public string Pipe { get; set; }
public string Version { get; set; }
}
Load your data into list to sort
List<YourClassName> list = souce of data;
list = SortList<YourClassName>(list, "PackageName");
SortList Method:
public List<YourClassName> SortList<TKey>(List<YourClassName> list, string sortBy)
{
PropertyInfo property = list.GetType().GetGenericArguments()[0].GetProperty(sortBy);
return list.OrderBy(e => property.GetValue(e, null)).ToList<YourClassName>();
}
This question already has answers here:
Natural Sort Order in C#
(18 answers)
Closed 4 years ago.
I am attempting to sort a List of fuses by the Designator property of the fuse.
The Fuse Model:
public class Fuse
{
public string Designator { get; set; }
public string Rating { get; set; }
public string Type { get; set; }
}
Example Data
This is an example of what a list of fuses would look like.
What I have tried:
fuses = fuses.OrderBy(f => f.Designator).ToList();
The above code sort of works, but it puts the F10 before F2. Does anyone know of a way to sort a List by the designator appropriately? I have read about natural sorting algorithms but not quite sure how to apply them correctly.
Any help would be greatly appreciated.
Thank you,
If you only compare string F2 the order will greater than F10.
You can try to order by number by Regex.
use ^F(\d+) pattern to get the number then order by.
fuses = fuses.OrderBy(f => int.Parse(Regex.Match(f.Designator, #"^F(\d+)").Groups[1].Value)).ToList();
You could implement IComparable<T>
class Fuse : IComparable<Fuse>
{
public string Designator { get; set; }
public int CompareTo(Fuse other)
{
if (string.IsNullOrWhiteSpace(other?.Designator)) return -1;
if (string.IsNullOrWhiteSpace(this.Designator)) return 1;
if (this.Designator == other.Designator) return 0;
// get the first item in the range
var d1Str = this.Designator.Split(new[] {',', '-'})[0];
var d2Str = other.Designator.Split(new[] {',', '-'})[0];
// parse to int
var d1 = int.Parse(d1Str.Substring(1));
var d2 = int.Parse(d2Str.Substring(1));
return d1 > d2 ? 1 : -1;
}
}
Test code
var list = new List<Fuse>()
{
new Fuse{Designator = "F8,F9"},
new Fuse{Designator = "F1,F2,F3"},
new Fuse{Designator = "F10-F12"},
new Fuse{Designator = "F4-F7"},
};
foreach (var itm in list.OrderBy(_ => _))
{
Console.WriteLine(itm.Designator);
}
Console.ReadLine();
Output
F1,F2,F3
F4-F7
F8,F9
F10-F12
You can try to sort it by integer instead. Throw away first letter, parse as integer and sort by this integer.
f => Int32.Parse(f.Designator.Substring(1))
Of course you can always refactor it out of lambda into something else.
Question moved here.
My requirement is to write a program that sort of mimics diff tools. Yes there are quite a few libraries and open source code that accomplishes this purpose, but I would like to write my own comparer.
Here's the starting point. I have a class called DataItem which looks like this:
public class DataItem
{
public DataItem() { }
public DataItem(string d, string v) { Data = d; Value = v; }
public string Data { get; set; }
public string Value { get; set; }
}
I have two lists of these class objects, let's call them PRE and POST and take some example values as follows. 'Data' part will be unique in a list.
preList: (Data,Value)
AAA,0
BBB,1
CCC,3
DDD,4
FFF,0
GGG,3
postList: (Data,Value)
AAA,0
BBB,2
DDD,4
EEE,9
FFF,3
Think of PRE as the original list, and POST as the list after some changes done. I would like to compare the two, and categorize them into three categories:
Added Items - An item with a new 'Data' added to the list.
Removed Items - An item was removed from the list.
Diff Items - 'Data' is found in both PRE and POST lists, but their corresponding 'Value' is different.
So when categorized they should look like this:
Added Items:
EEE,9
Removed Items:
CCC,3
GGG,3
Diff Items:
BBB
FFF
I have another DiffItem class, to objects of which I would like to put the final results. DiffItem looks like this:
public class DiffItem
{
public DiffItem() { }
public DiffItem(string data, string type, string pre, string post) { Data = data; DiffType = type; PreVal = pre; PostVal = post; }
public string Data { get; set; }
public string DiffType { get; set; } // DiffType = Add/Remove/Diff
public string PreVal { get; set; } // preList value corresponding to Data item
public string PostVal { get; set; } // postList value corresponding to Data item
}
To accomplish this, first I extended IEqualityComparer and wrote a couple of comparers:
public class DataItemComparer : IEqualityComparer<DataItem>
{
public bool Equals(DataItem x, DataItem y)
{
return (string.Equals(x.Data, y.Data) && string.Equals(x.Value, y.Value));
}
public int GetHashCode(DataItem obj)
{
return obj.Data.GetHashCode();
}
}
public class DataItemDataComparer : IEqualityComparer<DataItem>
{
public bool Equals(DataItem x, DataItem y)
{
return string.Equals(x.Data, y.Data);
}
public int GetHashCode(DataItem obj)
{
return obj.Data.GetHashCode();
}
}
Then used Except() and Intersect() methods as follows:
static void DoDiff()
{
diffList = new List<DiffItem>();
IEnumerable<DataItem> preOnly = preList.Except(postList, new DataItemComparer());
IEnumerable<DataItem> postOnly = postList.Except(preList, new DataItemComparer());
IEnumerable<DataItem> common = postList.Intersect(preList, new DataItemComparer());
IEnumerable<DataItem> added = postOnly.Except(preOnly, new DataItemDataComparer());
IEnumerable<DataItem> removed = preOnly.Except(postOnly, new DataItemDataComparer());
IEnumerable<DataItem> diffPre = preOnly.Intersect(postOnly, new DataItemDataComparer());
IEnumerable<DataItem> diffPost = postOnly.Intersect(preOnly, new DataItemDataComparer());
foreach (DataItem add in added)
{
diffList.Add(new DiffItem(add.Data, "Add", null, add.Value));
}
foreach (DataItem rem in removed)
{
diffList.Add(new DiffItem(rem.Data, "Remove", rem.Value, null));
}
foreach (DataItem pre in diffPre)
{
DataItem post = diffPost.First(x => x.Data == pre.Data);
diffList.Add(new DiffItem(pre.Data, "Diff", pre.Value, post.Value));
}
}
This does work and gets the job done. But I'm wondering if there's a 'better' way to do this. Note that I put quotes around the word 'better', because I don't have a proper definition for what would make this better. Perhaps is there a way to get this done without as many 'foreach' loops and use of Except() and Intersetc(), since I would imagine that behind the Linq there's quite a bit of iterations going on.
Simply put, is there a cleaner code that I can write for this? I'm asking mostly out of academic interest and to expand my knowledge.
I don't think you need your IEqualityComparer:
var added = from a in postList
where !preList.Any(b => b.Data == a.Data)
select new DiffItem(a.Data, "Add", null, a.Value);
var removed = from b in preList
where !postList.Any(a => a.Data == b.Data)
select new DiffItem(b.Data, "Remove", b.Value, null);
var diff = from b in preList
join a in postList on b.Data equals a.Data
where b.Value != a.Value
select new DiffItem(b.Data, "Diff", b.Data, a.Data);
var diffList = added.ToList();
diffList.AddRange(removed);
diffList.AddRange(diff);
I am trying to add a few rows I got from a DataTable to my list using this struct:
protected struct roleProperties
{
public string roleName { get; set; }
public string[] functionTitle { get; set; }
}
As you can see I want more strings inside the method Title string
I have been trying to do it like this:
public void getRoleFuncs(int roleId)
{
List<roleProperties> roles = new List<roleProperties>();
int i = 1;
SqlParameter ro_id = new SqlParameter("#ro_id", roleId);
string q = "SELECT ro_name, fu_title FROM roles INNER JOIN rolefunctions ON roles.ro_id = rolefunctions.fk_role_id INNER JOIN functions ON rolefunctions.fk_func_id = functions.fu_id WHERE ro_id = #ro_id";
SqlDataReader r = gm.returnReader(q, ro_id);
while (r.Read())
{
roleProperties item = new roleProperties();
item.roleName = r["ro_name"].ToString();
foreach (IDataRecord str in r)
{
item.functionTitle[i] = r["fu_title"].ToString();
i++;
}
roles.Add(item);
}
}
But I get a null reference on this line:
item.functionTitle[i] = r["fu_title"].ToString();
Can anyone see what I am doing wrong?
item.functionTitle is null because arrays are reference types and you have not initialized the property anywhere (so it has the default value: null for a reference type).
Even if that was not a problem (let's say functionTitle is an empty array) item.functionTitle[i] would again throw because it tries to access an index that is out of bounds. And finally, you have an off-by-one error: the first element in an array has the index 0, not 1.
You can fix all of the above by changing the code to
while (r.Read())
{
roleProperties item = new roleProperties();
item.roleName = r["ro_name"].ToString();
item.functionTitle = r.Select(o => o["fu_title"].ToString()).ToArray();
roles.Add(item);
}
Your array is not initialized and hence null since you do not know the size of the array you are going to need it seems a more suitable approach to use a list instead
change your struct to
protected class roleProperties
{
public string roleName { get; set; }
public IList<string> functionTitle { get; private set;}
public roleProperties(){
functionTitle = new List<string>();
}
}
and then change
item.functionTitle[i] = r["fu_title"].ToString();
to
item.functionTitle.Add(r["fu_title"].ToString());
I've changed the struct to a class because it's mutable and mutable structs are evil.
Initialize the array first.
item.functionTitle = new string[n]; // where n is an int