用linq选择匿名委托

时间:2011-02-04 16:28:55

标签: c# linq delegates

我知道有办法做到这一点,但我一直在撞墙试图解决这个问题。这很好用:

private GenericRecord CreateGeneric(GenericRecord g, Member m)
{
    g.Member = m;
    return g;
}

public IList<GenericRecord> ReportFromDatabase(DateTime startDate, DateTime endDate)
{
    List<GenericRecord> returnRecords = new List<GenericRecord>();

    returnRecords.AddRange(from r in pjRepository.Records
                           join m in memberRepository.Members on r.SID equals m.MemberId.ToString()
                           where r.TransactionDate >= startDate && r.TransactionDate <= endDate
                           select CreateGeneric((GenericRecord)r, m));

    return returnRecords;
}

但我知道有一种方法可以在没有CreateGeneric函数的情况下完成它。如何内联选择代理功能?

returnRecords.AddRange(from r in pjRepository.Records
                       join m in memberRepository.Members on r.SID equals m.MemberId.ToString()
                       where r.TransactionDate >= startDate && r.TransactionDate <= endDate
                       select (delegate
                       {
                           GenericRecord g = (GenericRecord)r;
                           g.Member = m;
                           return g;
                       }));

这给了我这个例外:

The type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'.

编辑:另一次尝试失败

returnRecords.AddRange((from r in pjRepository.Records
                        join m in memberRepository.Members on r.SID equals m.MemberId.ToString()
                        where r.TransactionDate >= startDate && r.TransactionDate <= endDate
                        select new { r, m }).Select(x =>
                        {
                            GenericRecord g = (GenericRecord)x.r;
                            g.Member = x.m;
                            return g;
                        }));

这给了我:

  

带有语句主体的lambda表达式无法转换为表达式树。

2 个答案:

答案 0 :(得分:12)

尝试:

returnRecords.AddRange((from r in pjRepository.Records
                            join m in memberRepository.Members on r.SID equals m.MemberId.ToString()
                            where r.TransactionDate >= startDate && r.TransactionDate <= endDate
                            select new { r, m }).AsEnumerable().Select(x =>
                            {
                                GenericRecord g = (GenericRecord)x.r;
                                g.Member = x.m;
                                return g;
                            }));

关键不同的是AsEnumerable()函数。这需要一个IQueryable并返回一个IEnumerable,它在幕后迫使Linq提供者对表达式树进行评估。这可以防止Linq库尝试将第二个Select的lambda转换为表达式树的一部分(它说它不能);第二个Select将通过评估IQueryable表达式树对生成的实际内存集合执行转换。由于您需要在语句结束之前评估查询(因此可以将所有元素添加到returnRecords),因此应该没有显着的性能差异。

答案 1 :(得分:0)

Seem like this is what you are looking at:
class Bila
    {
        public string Name;
        public List<string> Adresses;
    }
     var justNumbers = Enumerable.Range(0, 10);
Func<int,List<string>> foo = delegate(int j)
    {
        List<string> lst = new List<string>();
        for (int kk = 0; kk < j; kk++)
        {
            lst.Add("String_" + kk.ToString());
        }
        return lst;
    };
var zilla3 = (from x in justNumbers
              select new Bila
              {
                  Name = "Name_" + x.ToString(),
                  Adresses = foo(x),
              }).ToArray();