Try to use static method calls in queries

Posts   
 
    
Posts: 13
Joined: 30-Mar-2009
# Posted on: 31-Aug-2009 11:31:23   

I try to do things like the following example explains:

the query q works, the query q2 fails with an exception. I attached a little demo project with an sql Server example Database.

the intention is to pass an entity to an static formatting method to set info (string) for the resulting object:

namespace LLBLGEN_Test2
{
  class Program
  {
    static void Main(string[] args)
    {
      using (var adapter = new DataAccessAdapter())
      {
        var metaData = new LinqMetaData(adapter);

        var q = (metaData.TableB.Select(x => new TabB
                                               {
                                                 ID = x.Id,
                                                 Name = x.Name,
                                                 TabA = FormatTabAName(x.TableA),
                                               })).ToList();

        var q2 = (metaData.TableA.Select(x => new TabA
                                               {
                                                 ID = x.Id,
                                                 Name = FormatTabAName(x),

                                               })).ToList();
      }
    }

    private static string FormatTabAName(TableAEntity e)
    {
      if (e != null && !string.IsNullOrEmpty(e.Name))
        return string.Format("My name is {0}", e.Name);

      return string.Empty;
    }
  }

  internal class TabA
  {
    public Guid ID { get; set; }
    public string  Name { get; set; }
  }

  internal class TabB
  {
    public Guid ID { get; set; }
    public string Name { get; set; }
    
    public string TabA { get; set; }
  }

}

Why can use a sub property or related entity but not the entity itself ??

hope you can help me....

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 31-Aug-2009 16:21:58   

This doesn't work, as x is not yet materialized into an entity when it's passed to the FormatTabAName() method.

You can't pass variables which are of an entity type to in-memory methods inside a linq query which runs on the db.