0

I am having below class which contains some records :

public class File
{
    public string Name { get; set; }
    public List<Coordinates> Coordinates { get; set; }
}

Now this Name contains string like below:

1st record :  "TestFile-Dump3.xlsx.sheet1,TestFile-Dump2.xlsx.sheet,TestFile-Dump1.xlsx.sheet1",
2nd record :  "TestFile-Dump2.xlsx.sheet1",
3rd record :  "TestFile-Dump2.xlsx.sheet3,TestFile-Dump1.xlsx.sheet5"

Suppose my current file name is : TestFile-Dump3.xlsx.sheet1

var list = List<File>();
var File = list.FirstOrDefault(ls => ls.Name == "TestFile-Dump3.xlsx.sheet1"); //null

I want to find list of co-ordinates for above file name only but when i try to find i am getting null for file TestFile-Dump3.xlsx.sheet1.

Can anybody tell me what is the problem and how to do this with linq???

Backs
  • 24,430
  • 5
  • 58
  • 85
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • The problem is that the first record contains this file-name as a **substring** and not as a whole string – Tim Schmelter Jan 02 '17 at 10:27
  • Use StartsWith. ls.Name.StartsWith("TestFile-Dump3.xlsx.sheet1") – Balaji Marimuthu Jan 02 '17 at 10:27
  • 1
    Other users already suggested to use `Contains` or `StartsWith` to check for substrings, but be also aware, that you should compare strings with the `Equals`-method instead of the == operator, since the operation performs reference comparison (http://stackoverflow.com/a/3678810/3950370). – Lukas Körfer Jan 02 '17 at 10:35
  • In this case ```==``` is completely fair and words as expected. The answer you linked to discusses the differences between ```==``` and ```Equals()```, but I don't see how they apply here. ```==``` doesn't compare references here. String uses operator overloading to map ```==``` directly to ```Equals()``` – Benjamin Podszun Jan 02 '17 at 10:48

3 Answers3

2

Well, the first record contains the file-name, but as a substring. Since they are separated by commas you could use String.Split and Enumerable.Contains:

var File = list
    .FirstOrDefault(ls => ls.Name.Split(',').Contains("TestFile-Dump3.xlsx.sheet1")); 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

Your example records don't contain a record with a Name of 'TestFile-Dump3.xlsx.sheet1'. The only ~matching~ one is the first, but that has a Name that only contains the string you are looking for.

In other words: Would it help to replace the == with a call to Contains to solve your problem? Or maybe you want to Split() the Name and compare the parts?

list.FirstOrDefault(ls => ls.Name.Contains("TestFile-Dump3.xlsx.sheet1"));

or

list.FirstOrDefault(ls => ls.Name.Split(',').Contains("TestFile-Dump3.xlsx.sheet1"));
Benjamin Podszun
  • 9,679
  • 3
  • 34
  • 45
1

The first record name doesn't equal to the whole string. so use startswith or contains.

list.FirstOrDefault(ls => ls.Name.Contains("TestFile-Dump3.xlsx.sheet1");

list.FirstOrDefault(ls => ls.Name.StartsWith("TestFile-Dump3.xlsx.sheet1");
Balaji Marimuthu
  • 1,940
  • 13
  • 13