使用字符串输入查询对象结构

时间:2013-05-24 04:03:59

标签: c# linq dynamic

我正在尝试找到一种简洁的方法来允许输入可以查询对象结构的字符串。我认为动态linq查询是我想要的,但我无法弄清楚如何实现它。

用户将输入类似

的字符串

relationship.IsHappy = true 要么 relationship.Type =“叔叔” 要么 relationship.Type =“Uncle”&& relationship.IsHappy = true

main()中的最后两行是我正在寻找的解决方案:

string zQuery = args[0];
me.Realtionships.Where(zQuery); 

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic;

namespace LinqTest1
{
    class Program
    {
        static void Main(string[] args)
        {

            person me = new person();
            me.FirstName = "Andy";
            me.Realtionships = new List<relationship>();

            person aunt = new person();
            aunt.FirstName = "Lucy";

            relationship rAunt = new relationship();
            rAunt.IsHappy = true;
            rAunt.Type = "Aunt";
            rAunt.Person = aunt;
            me.Realtionships.Add(rAunt);

            person uncle = new person();
            uncle.FirstName = "Bob";

            relationship rUncle = new relationship();
            rUncle.IsHappy = false;
            rUncle.Type = "Aunt";
            rUncle.Person = uncle;
            me.Realtionships.Add(rUncle);

            string zQuery = args[0];
            me.Realtionships.Where(zQuery); 

        }
    }


    public class person
    {

        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        private List<relationship> _realtionships;
        public List<relationship> Realtionships 
        {
            get { return _realtionships; }
            set { _realtionships = value; }
        }

    }

    public class relationship 
    {
        private string _type;
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        private bool _isHappy;
        public bool IsHappy
        {
            get { return _isHappy; }
            set { _isHappy = value; }
        }

        private person _person;
        public person Person
        {
            get { return _person; }
            set { _person = value; }
        }
    }

}

4 个答案:

答案 0 :(得分:0)

你可以使用:

构建表达式树有一点涉及但最终更强大。当你完成后,你会吃掉并睡觉Lambda。 Dynamic Linq库允许您将字符串传递给解析字符串并创建Lambda表达式的工具。这更容易使用。

答案 1 :(得分:0)

有一个名为dynamic Linq:http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx的库,它允许您使用字符串而不是Linq查询的表达式树。

答案 2 :(得分:0)

我们使用表达式树序列化器/反序列化器来完成此操作。我相信你的情况你只需要反序列化传入的字符串,我们使用了这个http://expressiontree.codeplex.com/

答案 3 :(得分:0)

感谢。我失踪的那篇文章是AsQueryable()。见下面的解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic;

namespace LinqTest1
{
    class Program
    {
        static void Main(string[] args)
        {

            person me = new person();
            me.FirstName = "Andy";
            me.Realtionships = new List<relationship>();

            person aunt = new person();
            aunt.FirstName = "Lucy";

            relationship rAunt = new relationship();
            rAunt.IsHappy = true;
            rAunt.Type = "Aunt";
            rAunt.Person = aunt;
            me.Realtionships.Add(rAunt);

            person uncle = new person();
            uncle.FirstName = "Bob";

            relationship rUncle = new relationship();
            rUncle.IsHappy = false;
            rUncle.Type = "Uncle";
            rUncle.Person = uncle;
            me.Realtionships.Add(rUncle);

            //string zQuery = args[0];

            var res = me.Realtionships.AsQueryable().Where("Type == \"Uncle\"");
            foreach (relationship item in res)
            {
                Console.WriteLine(item.Person.FirstName); 
            }

            Console.ReadLine();

        }
    }


    public class person
    {

        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        private List<relationship> _realtionships;
        public List<relationship> Realtionships 
        {
            get { return _realtionships; }
            set { _realtionships = value; }
        }

    }

    public class relationship : IEnumerable<relationship> 
    {
        private string _type;
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        private bool _isHappy;
        public bool IsHappy
        {
            get { return _isHappy; }
            set { _isHappy = value; }
        }

        private person _person;
        public person Person
        {
            get { return _person; }
            set { _person = value; }
        }

        public IEnumerator<relationship> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

}