1

I feel stupid. Can you guys help me to replace GetCamp(x) with anonymous?

This code:

   aspnet_Users.ForEach(x =>
        {
            usersVm.Add(new User{
                 Camp = Mapper.Map<DbCamp, Camp>(GetCamp(x)),
            });
        }); 


    private DbCamp GetCamp(aspnet_Users x)
    {
        //... some code ...
        return someDbCamp;
    }

Should be something like this:

   aspnet_Users.ForEach(x =>
    {
         usersVm.Add(new User{
              Camp = Mapper.Map<DbCamp, Camp>
              (
                    Func<DbCamp>(aspnet_Users u) => 
                    { 
                         //... some code ...
                          return someDbCamp;
                    }
               ),
           });
     }); 

That doesn't work because Mapper.Map<Database.Camp, Camp> expects an object typeof(DbCamp) as a parameter, not a delegate. I can use a normal function of course but from academical standpoint, I wonder if it's possible to use anonymous method here.

iLemming
  • 34,477
  • 60
  • 195
  • 309

3 Answers3

1

I think this should handle the empty case.

x => x.Users.Any() ? x.Users.First().Camp : null

In context:

_dataContext.aspnet_Users.ToList().ForEach(x =>
    {
        usersVm.Add(new User{
             Camp = Mapper.Map<Database.Camp, Camp>(
                  x => x.Users.Any() ? x.Users.First().Camp : null),
        });
    }); 
recursive
  • 83,943
  • 34
  • 151
  • 241
  • It works although still doesn't answer the question... How to make it anonymous... using Func<> or whatever – iLemming May 26 '11 at 19:14
  • I'm pretty sure that you can just paste that in place of GetCamp(x). Is that not what you meant? – dlev May 26 '11 at 19:16
  • I mean the question is academical... If I needed to check for null I would just ask for that. I need to replace entire GetCamp() with anonymous. Regardless of that anonymous method functionality. Updated the question again – iLemming May 26 '11 at 19:23
  • @Agzam: It can be used anonymously. I updated my answer to show how. – recursive May 26 '11 at 19:41
  • I want something like Camp = Mapper.Map( x => { //here goes something } But it doesn't let me – iLemming May 26 '11 at 20:11
  • It says Cannot convert lambda expression to type 'Database.Camp' because it is not a delegate type – iLemming May 26 '11 at 20:14
  • @Agzam: You'll have to provide more code. It shouldn't be attempting to convert the lambda to a Camp. There must be something else going on in some of the other code. – recursive May 26 '11 at 20:49
  • Mapper.Map() expects Database.Camp object as a parameter. Not lambda not a delegate. I believe if I can leverage that to outer function which returns Database.Camp object, I can transform the outer function into an anonymous method. Using Func<> or something like that. I just don't know how it should look like syntactically – iLemming May 26 '11 at 21:27
  • @Agzam: I don't understand what you mean by outer function or where you want to use an anonymous method. Without more context, this is the best I can do, although I can see now that there's a type error in it. – recursive May 26 '11 at 22:06
0

Does this not work:

x => GetCamp(x)

?

Milimetric
  • 13,411
  • 4
  • 44
  • 56
  • I need to have a check because FirstOrDefault can be null. I updated the question – iLemming May 26 '11 at 18:52
  • You shouldn't update questions like that... should just close/accept the answer and make a new question. Otherwise you end up with useless answers hanging off of questions they don't seem to fit. – Milimetric May 26 '11 at 20:04
  • Edited, is that what you're looking for?? – Milimetric May 26 '11 at 20:05
  • I want to transform GetCamp function into an anonymous something like this: () => { return Camp}. this doesn't work of course because Mapper.Map() expects Database.Camp object as a parameter. Not lambda expression, not a delegate – iLemming May 26 '11 at 21:30
0
aspnet_Users.ForEach(x =>
{
    usersVm.Add(new User{ Camp = Mapper.Map<DbCamp, Camp>({ /*... some code using x ...*/ return someDbCamp; }) });
});
Ssithra
  • 710
  • 3
  • 8