开发者

Using a string in a LINQ query

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


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

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

before the line

var query = ...


Try trimming the string both at the beginning and end for possible whitespace which would fail the string match.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜