Explicit expression for XAttribute value

I recently wrote a code snippet that looked something like this:

IEnumerable<DTO.Employee> xEmployee =
    from e in xDoc.Descendants("Employee")
    where int.Parse(e.Attribute("Id").Value) == emp.Id
    select new DTO.Employee
    {
        Id = (int)e.Attribute("Id"),
        LastName = (string)e.Element("LastName"),
        FirstName = (string)e.Element("FirstName"),
        Email = (string)e.Element("Email")
    };

However, I am confused about casting to int in the where clause. First, I wrote something like

where (int)(e.Attribute("Id").Value) == emp.Id

which did not compile. Why can I explicitly press (e.Attribute ("Id")) but cannot do it (e.Attribute ("Id"). Value)?

+3
source share
2 answers

Check for explicit overloads of XAttribute class statements .

public static explicit operator int(XAttribute attribute);
+4
source

XAttribute int - string ( XAttribute.Value) int.

+1

Source: https://habr.com/ru/post/1726729/


All Articles