Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class has invalid expression term "from" when querying using LINQ

I have the following serializable class:

[Serializable]
public class EmailClass
{
    public string from;
    public List<string> To;
    public List<string> CC;
    public string DisplayTo;
    public string Subject { get; set; }
    public int attachments;      
    public List<string> attachmentsName;
    public string DateTimeReceived;
    public string DateTimeSent;
    public string FinalFilename;
    public string DatetimeCreated;
    public string ExchangeUniqueId;
    public string ChankeyID;
    public string  FinalFileName {get;set;}
    public bool Encrypted;
    public string Descripcion { get; set; }

}

So know, I have a List and I want to query it using Linq:

 var query = from p in listado 
             where p.from.ToString().ToUpper() == textBox1.Text

And the problem is that it recognizes the p.from as the linq from identifier, so it is not accepted.

Error: Invalid expression term 'from'

I can not change the class public string "from" because it is deserializing xml objects stored in hard disk.

How could I work around this problem?

I know I could use List.FindAll or something like that, but I would really like to know if I can do it using linq.

like image 550
Carlos Landeras Avatar asked Dec 12 '22 10:12

Carlos Landeras


2 Answers

Change where p.from.ToString() to where [email protected](). You are using from as both a keyword and identifier.

like image 187
David Arno Avatar answered Dec 13 '22 23:12

David Arno


David Arno has shown how to avoid conflicts with keywords via @from, however:

I can not change the class public string "from" because it is deserializing xml objects stored in hard disk.

As an alternative approach:

[XmlElement("from")]
public string From {get;set;}

This tells XmlSerializer how to map the name; then you can use:

var query = from p in listado 
            where p.From.ToUpper() == textBox1.Text
like image 25
Marc Gravell Avatar answered Dec 13 '22 22:12

Marc Gravell