Select anonymous delegate with linq

I know there is a way to do this, but I hit my head against the wall, trying to figure it out. This works great:

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;
}

But I know that there is a way to do this without the CreateGeneric function. How to choose inline delegate function?

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;
                       }));

This gives me this exception:

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

Edit: Another unsuccessful attempt

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;
                        }));

This gives me:

A lambda expression with an operator body cannot be converted to an expression tree.

+3
source share
2 answers

Try:

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 im-memory, IQueryable. , , ( returnRecords), .

+12
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();
0

Source: https://habr.com/ru/post/1790021/


All Articles