Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据集合将字符串转换为Lambda表达式_C#_Lambda - Fatal编程技术网

C# 根据集合将字符串转换为Lambda表达式

C# 根据集合将字符串转换为Lambda表达式,c#,lambda,C#,Lambda,如何将针对集合的属性选择器的字符串表示形式转换为lambda表达式 输入的格式为: "Child.Name" 我需要将其转换为一个谓词,以便在OrderBy子句中使用 query.OrderBy(x => x.Child.FirstOrDefault().Name); 如果属性不是集合,是否也可能有一个有效的解决方案,例如针对标准属性或字段 "Quantity" 将创建一个谓词,例如 x => x.Quantity 编辑: 只是想改写上面的问题,如果我可以改变输入字符串,是否

如何将针对集合的属性选择器的字符串表示形式转换为lambda表达式

输入的格式为:

"Child.Name"
我需要将其转换为一个谓词,以便在OrderBy子句中使用

query.OrderBy(x => x.Child.FirstOrDefault().Name);
如果属性不是集合,是否也可能有一个有效的解决方案,例如针对标准属性或字段

"Quantity"
将创建一个谓词,例如

x => x.Quantity
编辑:

只是想改写上面的问题,如果我可以改变输入字符串,是否可以执行

"Child.FirstOrDefault().Name"
作为


在OrderBy中?

我认为首先需要使用以下类型按名称获取类的实例:

然后,您可以通过以下方式按名称获取属性:

然后可以将这些类型传递给查询


或者,您可以使用

有一个应用程序可以解决您的问题。试试看,告诉我一些事情

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// Punto de entrada principal para la aplicación.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            List<Parent> Parents = new List<Parent>();
            List<Parent> orderedParents = Parents.Cast<Parent>().OrderBy(x => x.Cast<Child>().Select(y => y.myName)).ToList();
        }
    }

    public class Parent: IQueryable 
    {
        List<Child> child = new List<Child>();

        #region Miembros de IQueryable

        public Type ElementType
        {
            get { throw new NotImplementedException(); }
        }

        public System.Linq.Expressions.Expression Expression
        {
            get { throw new NotImplementedException(); }
        }

        public IQueryProvider Provider
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region Miembros de IEnumerable

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

        #endregion
    }

    public class Child: Parent 
    {
        public string myName;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
静态类程序
{
/// 
///因特拉达校长帕拉·阿普利卡西翁(Punto de entrada Principa la aplicación)。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
列表父项=新列表();
List orderedParents=Parents.Cast().OrderBy(x=>x.Cast().Select(y=>y.myName)).ToList();
}
}
公共类父级:IQueryable
{
列表子项=新列表();
#Miembros de IQueryable地区
公共类型ElementType
{
获取{抛出新的NotImplementedException();}
}
public System.Linq.Expressions.Expression表达式
{
获取{抛出新的NotImplementedException();}
}
公共IQueryProvider提供程序
{
获取{抛出新的NotImplementedException();}
}
#端区
#Miembros de IEnumerable地区
public System.Collections.IEnumerator GetEnumerator()
{
抛出新的NotImplementedException();
}
#端区
}
公共类子级:父级
{
公共字符串myName;
}
}

以下内容将返回集合上属性的字符串值:

string propertyValue = source.GetType()
                .GetProperty(stringProperty)
                .GetValue(query.FirstOrDefault(), null) as string;

你可能应该调查“Child”对象的类型是什么?你说的“我需要把它转换成谓词”是什么意思?您是否已经有了一个具有
Child
collection属性的对象?@MGG\u Soft Child是类对象的集合。@YuvalItzchakov查询是对象的IQueryable,每个对象都包含一个名为Child的属性,该属性包含类对象的集合。为什么要通过实例化对象来获得类型
var childType=Type.GetType(“NAMESPACE.Child,ASSEMBLY_NAME”)var childType = (typeof)child;
var name = childType.GetProperty("Name");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// Punto de entrada principal para la aplicación.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            List<Parent> Parents = new List<Parent>();
            List<Parent> orderedParents = Parents.Cast<Parent>().OrderBy(x => x.Cast<Child>().Select(y => y.myName)).ToList();
        }
    }

    public class Parent: IQueryable 
    {
        List<Child> child = new List<Child>();

        #region Miembros de IQueryable

        public Type ElementType
        {
            get { throw new NotImplementedException(); }
        }

        public System.Linq.Expressions.Expression Expression
        {
            get { throw new NotImplementedException(); }
        }

        public IQueryProvider Provider
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region Miembros de IEnumerable

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

        #endregion
    }

    public class Child: Parent 
    {
        public string myName;
    }
}
string propertyValue = source.GetType()
                .GetProperty(stringProperty)
                .GetValue(query.FirstOrDefault(), null) as string;