0

I have a list of objects that returned from the DB, I am using linq to compare specific value with property in the list , and if the value found in the list with percent 80% or above, then return repeated item else return the object.

I write code but it's return repeated if the value as same as property.

List<CatalogDTO> lst=new List<CatalogDTO>();
var catalogname="Iphone s";
 List<CatalogDTO> lstCatalogs= new List<CatalogDTO>();
        lstCatalogs.Add(new CatalogDTO{ Id=1,CatalogName ="Iphone red"});
        lstCatalogs.Add(new CatalogDTO{ Id=2,CatalogName ="Iphone green"});
        lstCatalogs.Add(new CatalogDTO{ Id=3,CatalogName ="iphone x"});
        lstCatalogs.Add(new CatalogDTO{ Id=4,CenterNameAr="iphone xs"});
var catalog = lstCatalogs.where(x=> x.CatalogName == catalogname).FirstOrDefault();
if(catalog !=null){
    return "Repeated Catalog";
}
else{
    lst.Add(catalog);
}

The code above must return objects with Ids 3 and 4 because the similarity about 80% or more (means the percent of characters similarity as all words in the string except one for example ).

  • 2
    Perhaps you want to do something such as calculating the Levenshtein distance? – ProgrammingLlama Aug 08 '22 at 07:56
  • 2
    Searching and matching strings is hard, so hard that (successful) multi billion dollar companies have been founded around the whole concept. I recommend you check out some string comparison algorithms and decide on one for you to implement/ use. [Here](https://stackoverflow.com/a/1095806/9363973) is a SO answer listing some with a link to a GitHub project implementing these for reference – MindSwipe Aug 08 '22 at 07:57
  • 1
    You need to define what you mean by "80% or more". There are several ways of calculating difference between two strings. – Palle Due Aug 08 '22 at 07:58
  • may be he wants something like lstCatalogs.where(x=> x.CatalogName.StartwWith( catalogname)).ToList(); – thanzeel Aug 08 '22 at 08:04
  • As stated by @PalleDue you really need to define what "80% or more" means in this context. Is it the characters used? What about order? If I had 'Iphone x' and 'x enohpI' should they be matched? – sr28 Aug 08 '22 at 08:09
  • 1
    It's still not clear as you're not defining specific scenarios, such as how would string "ABC" match against "ABCDEF"? Is that a 100% match for the first string as all characters are present? What about "ABC" compared with "CBA" or compared with "A B C". When you say similarity does position in the alphabet matter e.g. would "CDE" match more closely to "ABC" than "XYZ"? – sr28 Aug 08 '22 at 09:31
  • @sr28 I mentioned that I compare word per word, case insensitive and spaces will count as word specifier So A B C contains 3 words so similarity will be 0 because no word matches. – Rawan Mansour Aug 08 '22 at 10:04

0 Answers0