オブジェクト構造を照会できる文字列の入力を許可するクリーンな方法を見つけようとしています。動的な linq クエリが必要だと思いますが、これを実装する方法がわかりません。
ユーザーは次のような文字列を入力します
relationship.IsHappy = true or relationship.Type = "おじさん" or relationship.Type = "おじさん" && relationship.IsHappy = true
main() の最後の 2 行は、私が解決策を見つけようとしているものです。
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; }
}
}
}