1

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

用户将输入一个字符串,如

relationship.IsHappy = true or relationship.Type = "Uncle" or 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

4 回答 4

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();
        }
    }

}
于 2013-05-24T11:57:00.113 回答
0

您可以使用 :

或者

  • 动态 Linq 查询 一个 Microsoft 库,其中包含您可以下载的源代码和示例。

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

于 2013-05-24T04:41:31.163 回答
0

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

于 2013-05-24T05:15:03.463 回答
0

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

于 2013-05-24T04:50:47.953 回答