score:1
You can get rid of the inner loop using System.Linq. Also simplify code by using foreach instead of for for main loop:
ConcurrentDictionary<string, string> compareDictionary = new ConcurrentDictionary<string, string>();
foreach (var element in list1)
{
var item1Name= element.Name.ToString();
var item1Id= element.ID.ToString();
// If this element has already one correspondence, TryAdd will fail anyway
if(compareDictionary.ContainsKey(item1Id)) continue;
var found = list2.FirstOrDefault(item => item1Name.Contains(item.item2Name.ToLower()));
if(found != null)
{
compareDictionary.TryAdd(item1Id, item.item2Id);
}
}
Depending on your elements naming, you also could need to make item1Name lower case. Your code will in fact find this correspondence:
Parentwithlittlechild --> LittleChild
but not this:
ParentWithLittleChild --> LittleChild
because "LittleChild" would be lower cased, and String.Contains works case sensitively by default.
You could also use IndexOf
method with StringComparison.OrdinalIgnoreCase
, like this:
var found = list2.FirstOrDefault(item => item1Name.IndexOf(item.item2Name, StringComparison.OrdinalIgnoreCase) >= 0);
Final consideration: element.Name
is probably already of type string, unless your Item1.Name
property is some funny type. If so, element.Name.ToString()
is convertible in just element.Name
.
score:1
Couple of code snippets to find the matches and add the ids
of the two objects in a ConcurrentDictionary<string, string>
.
So we have:
var dict = new ConcurrentDictionary<string, string>();
First
list1.ForEach(x =>
{
list2.Where(y => y.Item2Name.IndexOf(x.Name, StringComparison.CurrentCultureIgnoreCase) >= 0)
.ToList().ForEach(y => dict.TryAdd(x.ID, y.Item2Id));
});
Second
(from x in list1
from y in list2
where x.Name.IndexOf(y.Item2Name, StringComparison.CurrentCultureIgnoreCase) >= 0
select (x, y)).ToList().ForEach(item => dict.TryAdd(item.x.ID, item.y.Item2Id));
Side Note
If both
Item1.Name
andItem2.Item2Name
properties are ofstring
type, then no need to useToString()
function like invar item1Name= list1[i].Name.ToString();
line.If both
Item1.ID
andItem2.Item2Id
properties are ofint
type, then the proper type of bothKey
andValue
pairs of the dictionary is theint
type. So:
var dict = new ConcurrentDictionary<int, int>();
Source: stackoverflow.com
Related Articles
- Compare items in two lists and replace foreach loop with LINQ
- Replace foreach loop with linq
- Linq to format a list of items to replace foreach loop
- Simplifying a foreach loop with LINQ (selecting two objects in each iteration)
- how can compare items of two lists with linq?
- Replace for-switch loop with a Linq query
- Using Linq to compare a list with a set of lists in C#
- Replace two foreach loops with linq
- Compare items in different dictionaries with same key using LINQ
- Compare two object lists with LINQ on specific property
- convert foreach loop to linq code
- Is it possible to replace this foreach loop with Linq?
- Linq Filtering items with ForEach crashes for large data set
- Is there a better way to do this, maybe replace the for/foreach loop with something LINQ ish?
- Compare lists and get values with Linq
- Mapping two lists of points and replace with linq
- How/should I replace nested foreach with one linq query?
- Converting foreach loop to LINQ query breaks code
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- Is it possible to use Linq to replace this Foreach loop
- How to replace foreach with LINQ expressions
- How can I convert Foreach loop with Linq expression OR AsEnumerable in C#.net?
- Easiest way of comparing two lists in C# with LINQ and adding missing items to Entity Class
- ForEach loop with Lambda expression in Razor code MVC 5 For List<T>
- C# LINQ code for two list compare and replace
- how to turn nested foreach loop with C# linq
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- How to match two Lists with only items that are different in Linq
- how to get items from a dictionary of lists with linq
- Convert the code from Foreach loop to Linq
- Convert SQLServer sql to Linq
- Foreach delete with linq
- The Big O of Distinct() method with a Custom IEqualityComparer
- Adding condition (string) to Entity LINQ query
- System.InvalidCastException: Unable to cast object of type 'System.Linq.GroupedEnumerable
- Creating KML with Linq to XML
- Sitecore.FakeDB and Sitecore Content Search with facets
- comparing 2 lists of objects and return changes in the new list
- trying to convert sql to linq sql
- Comparing dates in LINQ which are stored as strings in database
- The model item passed into the dictionary is of type List, but this dictionary requires a model item of type IEnumerable
- How to debug a Linq Lambda Expression?
- Linq select result with duplicating rows
- Query date instead of datetime docdb
- Aggregate and Group by Datetime
- Custom Linq-to-SQL MappingSource
- The more "SQL-syntax" of Linq.Except
- convert object list to type array and remove null values
- flat list to hierarchy
- T-SQL to LINQ to SQL using Navigation Properties