搜索包含&的字符串时出错

时间:2011-04-12 14:26:08

标签: c# linq linq-to-objects

我正在使用linq查询来获取包含变量字符串的对象列表:

   string str = FilterAllSource;
                if (!string.IsNullOrEmpty(str))
                {
                    ss = ss.Where(it => (it.SourceCode.ToUpper()
                                               .Contains(str.ToUpper())
                                         || it.SourceName.ToUpper()
                                               .Contains(str.ToUpper())));
                }

                if (top > 0)
                    ss = ss.Take(top);

                return ss.ToList();

当FilterAllsource包含& (例如m& m)它抛出异常

Unterminated string literal at position 38 in '(ResourceId eq 3) and (substringof('M '.

我的猜测是&干扰一些内部串联过程。有没有办法避免这种情况而不删除&从查询字符串?

EDIT1:ss是一个对象列表,其中SourceCode和Sourcename都是字符串

2 个答案:

答案 0 :(得分:2)

这对我有用......

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        List<Test> list = p.GetList();
    }

    public List<Test> GetList()
    {
        List<Test> ss = new List<Test>();
        ss.Add(new Test("m & mm & mm & mm & mm & mm & mm & m", "m & mm & mm & mm & mm & mm & mm & m"));
        string str = "m & m";

        if (!string.IsNullOrEmpty(str)) 
        { 
            ss = ss.Where(it => (it.SourceCode.ToUpper().Contains(str.ToUpper()) || it.SourceName.ToUpper().Contains(str.ToUpper()))).ToList();
        } 

        //if (top > 0) 
        //{
        //    ss = ss.Take(top); 
        //}

        return ss.ToList(); 
    }
}

public class Test
{
    public Test(string sourceCode, string sourceName)
    {
        this.SourceCode = sourceCode;
    }

    public string SourceCode
    {
        get;
        set;
    }

    public string SourceName
    {
        get;
        set;
    }
}

答案 1 :(得分:1)

我无法重现它,我想这是在这段代码之前发生的。您是否可以在过滤之前实现ss,以检查代码示例是否已被隔离?

var itemsToFilter = ss.ToList();
string str = FilterAllSource;
            if (!string.IsNullOrEmpty(str))
            {

                itemsToFilter = itemsToFilter.Where(it => (it.SourceCode.ToUpper()
                                           .Contains(str.ToUpper())
                                     || it.SourceName.ToUpper()
                                           .Contains(str.ToUpper())));
            }

            if (top > 0)
                itemsToFilter = itemsToFilter.Take(top);

            return itemsToFilter.ToList();

我用linq尝试过这个对象,工作得很好:

class Program
{
    static void Main(string[] args)
    {
        var ss = new List<Dummy> { new Dummy
                     {
                         SourceCode = "m & m",
                         SourceName = "blabla"
                     }}.AsEnumerable();

        string str = "m & m";
        if (!string.IsNullOrEmpty(str))
        {
            ss = ss.Where(it => (it.SourceCode.ToUpper()
                                       .Contains(str.ToUpper())
                                 || it.SourceName.ToUpper()
                                       .Contains(str.ToUpper()))).ToArray();
        }

        var top = 2;

        if (top > 0)
            ss = ss.Take(top).ToArray();

        Console.WriteLine(ss.Count());
    }
}

public class Dummy
{
    public string SourceCode { get; set; }
    public string SourceName { get; set; }
}

还尝试使用linq to sql,不是相同的代码,但应该引发同样的问题(如果有问题):

class Program
{
    static void Main(string[] args)
    {
        string str = "&";
        using (var context = new DataClasses1DataContext())
        {
            var clients = context.Clients.Where(x => x.Code.Contains(str.ToUpper()));
            clients = clients.Take(5);
            Console.WriteLine(clients.Count());
        }          
    }
}