如何确定 LINQ 查询 SELECT 变量的位置

发布于 2024-10-15 14:09:34 字数 399 浏览 1 评论 0原文

是否可以通过不使用属性而是使用列名来使 LINQ SELECT 更加灵活? 也许一个例子会有所帮助..我正在尝试执行以下操作(伪代码):

From x In Entities
Where ...
Select("ID", "Value" , "Date")

但根据某些选择,我希望结果以不同的顺序

From x In Entities
Where ...
Select("Value", "Date", "ID" )

或其他数量的 SELECT 结果

From x In Entities
Where ...
Select("Value")

任何帮助使其尽可能动态太棒了! tnx

Is it possible to make the LINQ SELECT more flexible by not working with properties but with the column name?
Maybe an example will help..I'm trying to do the following (pseudocode):

From x In Entities
Where ...
Select("ID", "Value" , "Date")

but depending on certain choices, I would like to have the result in different order

From x In Entities
Where ...
Select("Value", "Date", "ID" )

Or another amount of SELECT results

From x In Entities
Where ...
Select("Value")

Any help to make this as dynamic as possible would be AWESOME! tnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-10-22 14:09:34

也许这会对你有帮助

from x In Entities
where ... select new {
  Value = x["Value"],
  Date = x["Date"],
  ID = x["ID"]
}

Maybe this will help you

from x In Entities
where ... select new {
  Value = x["Value"],
  Date = x["Date"],
  ID = x["ID"]
}
夜灵血窟げ 2024-10-22 14:09:34

就像我在评论中所说的那样(处理子属性,例如 Type.Name,但不处理多个字段)
我让乐趣来制作多字段版本;)

public static class DynamicLinkExtensions
{    
    public static IEnumerable<dynamic> Select<T>(this IQueryable<T> source, string memberAccess)
    {
        var propNames = memberAccess.Split('.');
        var type = typeof(T);
        var props = new List<PropertyInfo>();

        foreach (var propName in propNames)
        {
            var prop = type.GetProperty(propName);
            props.Add(prop);
            type = prop.PropertyType;
        }
        return source.Select(props.ToArray());
    }

    public static IEnumerable<dynamic> Select<T>(this IQueryable<T> source, PropertyInfo[] props)
    {
        var parameter = Expression.Parameter(typeof(T));
        var member = Expression.MakeMemberAccess(parameter, (MemberInfo)props.First());

        for (var i = 1; i < props.Length; i++)
        {
            member = Expression.MakeMemberAccess(member, (MemberInfo)props[i]);
        }

        Expression<Func<T, object>> expression = Expression.Lambda<Func<T, object>>(member, new[] { parameter });
        return source.Select(expression);
    }
}

用法:

var names = DataContext.Customers.Select("Name").Cast<string>().ToList();

Like I said in my comment (handles subproperties, like Type.Name, but not multiple fields)
I let the fun to make the multi field version ;)

public static class DynamicLinkExtensions
{    
    public static IEnumerable<dynamic> Select<T>(this IQueryable<T> source, string memberAccess)
    {
        var propNames = memberAccess.Split('.');
        var type = typeof(T);
        var props = new List<PropertyInfo>();

        foreach (var propName in propNames)
        {
            var prop = type.GetProperty(propName);
            props.Add(prop);
            type = prop.PropertyType;
        }
        return source.Select(props.ToArray());
    }

    public static IEnumerable<dynamic> Select<T>(this IQueryable<T> source, PropertyInfo[] props)
    {
        var parameter = Expression.Parameter(typeof(T));
        var member = Expression.MakeMemberAccess(parameter, (MemberInfo)props.First());

        for (var i = 1; i < props.Length; i++)
        {
            member = Expression.MakeMemberAccess(member, (MemberInfo)props[i]);
        }

        Expression<Func<T, object>> expression = Expression.Lambda<Func<T, object>>(member, new[] { parameter });
        return source.Select(expression);
    }
}

Usage:

var names = DataContext.Customers.Select("Name").Cast<string>().ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文