【问题标题】:Query Object Structure using string input使用字符串输入查询对象结构
【发布时间】:2013-05-24 04:03:59
【问题描述】:

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

用户会输入一个类似

的字符串

relationship.IsHappy = true 要么 关系.Type =“叔叔” 要么 relationship.Type = "叔叔" && 关系.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; }
        }
    }

}

【问题讨论】:

标签: c# linq dynamic


【解决方案1】:

你可以使用:

构建表达式树的过程稍微复杂一些,但最终更强大。完成后,您将吃饭和睡觉 Lambda。 Dynamic Linq 库允许您将字符串传递给解析字符串并创建 Lambda 表达式的工具。这更容易使用。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我们使用表达式树序列化器/反序列化器来做到这一点。我相信在你的情况下你只需要反序列化传入的字符串,我们使用了这个http://expressiontree.codeplex.com/

      【讨论】:

        【解决方案4】:

        谢谢。我缺少的部分是 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();
                }
            }
        
        }
        

        【讨论】:

        • 请注意,"Type == \"Uncle\"" 可能是输入参数。
        猜你喜欢
        • 1970-01-01
        • 2012-10-23
        • 2013-09-11
        • 2018-07-21
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多