动态Linq& ICompare [英] Dynamic Linq & ICompare

查看:137
本文介绍了动态Linq& ICompare的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图利用James McCormack的使用Dynamic Linq自定义IComparer的指南。 - 请参阅 http://zootfroot.blogspot .co.uk / 2009/10 / dynamic-linq-orderby.html?showComment = 1347276236930#c11348033278810583

I'm trying to make use of James McCormack's guide to using a custom IComparer with Dynamic Linq. - see http://zootfroot.blogspot.co.uk/2009/10/dynamic-linq-orderby.html?showComment=1347276236930#c11348033278810583

我使用查询来拉回一个Enumerable的字符串值:

I use a query to pull back an Enumerable of string values:

.Select("fieldname").Distinct()

然后尝试使用

.OrderBy(item=>item.GetReflectedPropertyValue("fieldname"),new myComparer()) 

GetReflectedPropertyValue是一个助手方法由James定义为

GetReflectedPropertyValue is a helper method defined by James as

public static string GetReflectedPropertyValue(this object subject, string field)
{
object reflectedValue = subject.GetType().GetProperty(field).GetValue(subject, null);
return reflectedValue != null ? reflectedValue.ToString() : "";
}

但是得到错误'System.Collections.Generic.IEnumerable'不包含'OrderBy'的定义和最佳的扩展方法重载'System.Linq.Dynamic.DynamicQueryable.OrderBy(System.Linq.IQueryable,string,params object [])'有一些无效的参数

But get the error "'System.Collections.Generic.IEnumerable' does not contain a definition for 'OrderBy' and the best extension method overload 'System.Linq.Dynamic.DynamicQueryable.OrderBy(System.Linq.IQueryable, string, params object[])' has some invalid arguments"

任何想法?我是新来的,只是想得到一些有用的东西,我才有时间去实际学习,正确地学习。

Any ideas? I'm new to this and just trying to get something working before I get the time to actually go through and learn it properly.

推荐答案

p>根据 OrderBy 的解决方案将取决于 myConverter 。但是,由于 GetReflectedPropertyValue 返回 string myConverter 必须实现要与 OrderBy 一起使用的IConverter< String> 。如果没有,并且它实现类似于 IComparer< MyItem> 的编译器,那么编译器真的在寻找一种方法 OrderBy(这个IEnumerable< object>

it's hard to tell based only on what you've posted because the resolution of OrderBy will depend on the implementation of myConverter. But, Since GetReflectedPropertyValue returns string, myConverter must implement IConverter<String> to be used with OrderBy. If it doesn't, and it implements something like IComparer<MyItem> then compiler is really looking for a method OrderBy(this IEnumerable<object>, Func<object,string>, IComparer<MyItem>) which doesn't exist and spits out that error.

如果不是这样,清楚,提供 myConverter 的详细信息,我可以更具体。

If that isn't clear, provide the details of myConverter and I can be more specific.

如果你使用的东西不不要执行 IComparer< T> ,那么你可以使用实现的东西包装 IComparer IComparable< string> ,以便与 Orderby 兼容。例如:

If you're using something that doesn't implement IComparer<T>, then you can wrap the IComparer with something that implements IComparable<string> so that it is compatible with Orderby. For example:

public class MyStringComparer : IComparer<String>
{
    readonly IComparer comparer = new MyComparer();
    public int Compare(string x, string y)
    {
        return comparer.Compare(x, y);
    }
}

这篇关于动态Linq&amp; ICompare的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆