在 LINQ 查询中使用字符串
我目前正在用 C# 开发一个 WPF 项目。该项目采用在页面初始化时定义的字符串 (newMemoryRFID) 并在查询中使用它。像这样
var query =
from c in MemoryData.Memory
where c.RFID == newMemoryRFID
select c;
this.DataContext = query;
this.View = ((CollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
这会产生一个空的 DataContext
但是,当我使用与 newMemoryRFID 相同的测试数据时,查询将是查询,即
var query =
from c in MemoryData.Memory
where c.RFID == "0F02D76B05"
select c;
this.DataContext = query;
this.View = ((CollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
查询获取正确的记录。正如您可能知道的那样,我不是最好的程序员,因此您的答案越简单越好。提前非常感谢
I am currently developing a WPF project in c#. The project takes a string (newMemoryRFID) which is defined when the page is initialised and uses it in a query. Like so
var query =
from c in MemoryData.Memory
where c.RFID == newMemoryRFID
select c;
this.DataContext = query;
this.View = ((CollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
This produces an empty DataContext
However when I use test data which is the same as what newMemoryRFID would be the query i.e.
var query =
from c in MemoryData.Memory
where c.RFID == "0F02D76B05"
select c;
this.DataContext = query;
this.View = ((CollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
The query gets the correct record. As you may be able to tell I'm not the best programmer so the simpler your answer the better. And thanks very much in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用调试器的时候了。听起来 newMemoryRFID 在创建查询时未设置为“0F02D76B05”。
如果你不能进去,至少
在排队之前进去
This is the time to use your debugger. It sounds like newMemoryRFID isn't set to "0F02D76B05" at the time that query is created.
If you can't step into it, at least do
before the line
尝试在开头和结尾处修剪字符串,以消除可能导致字符串匹配失败的空格。
Try trimming the string both at the beginning and end for possible whitespace which would fail the string match.