简体   繁体   English

在LINQ查询中使用字符串

[英]Using a string in a LINQ query

I am currently developing a WPF project in c#. 我目前正在用C#开发WPF项目。 The project takes a string (newMemoryRFID) which is defined when the page is initialised and uses it in a query. 该项目采用一个字符串(newMemoryRFID),该字符串在页面初始化时定义,并在查询中使用它。 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 这将产生一个空的DataContext

However when I use test data which is the same as what newMemoryRFID would be the query ie 但是,当我使用与newMemoryRFID相同的测试数据时,即

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 并非常感谢

This is the time to use your debugger. 现在是时候使用调试器了。 It sounds like newMemoryRFID isn't set to "0F02D76B05" at the time that query is created. 听起来好像在创建查询时newMemoryRFID没有设置为“ 0F02D76B05”。

If you can't step into it, at least do 如果您无法进入,至少要做

Debug.WriteLine(string.Format("newMemoryRFID = {0}", newMemoryRFID); 

before the line 行前

var query = ...

尝试在开头和结尾处都修剪字符串,以获取可能导致空格匹配失败的空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM