4

我有以下可序列化的类:

[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; }

}

所以知道,我有一个列表,我想使用 Linq 查询它:

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

问题是它将 识别p.from 为来自 identifier 的 linq,因此不被接受。

错误:无效的表达式术语“来自”

无法更改类公共字符串“from”,因为它正在反序列化存储在硬盘中的 xml 对象。

我该如何解决这个问题?

我知道我可以使用 List.FindAll 或类似的东西,但我真的很想知道我是否可以使用 linq 来做到这一点。

4

3 回答 3

8

更改where p.from.ToString()where p.@from.ToString()。您from同时用作关键字和标识符。

于 2013-10-21T09:46:00.763 回答
4

David Arno 展示了如何通过 避免与关键字冲突@from,但是:

我无法更改类公共字符串“from”,因为它正在反序列化存储在硬盘中的 xml 对象。

作为替代方法:

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

这告诉XmlSerializer了如何映射名称;那么你可以使用:

var query = from p in listado 
            where p.From.ToUpper() == textBox1.Text
于 2013-10-21T09:49:18.007 回答
1

您可以改用方法链:

var query = listado.Where(p => p.from.ToString().ToUpper() == textBox1.Text);
于 2013-10-21T09:47:19.490 回答