1

Consider this:

var query = (from u in entity.Users
             select new
                    {
                        FullName = u.FirstName + " " + u.LastName
                    }
            );

Which works fine, but what I want to do is this:

var query = (from u in entity.Users
                select new
                {
                    FullName = u.FullName
                }
            );

I am using Metadata which returns (u.FirstName + " " + u.LastName)

[NotMapped]
public string FullName
{
    get
    {
        return FirstName + " " + LastName;
    }
}

But I am getting an error:

The specified type member 'FullName' is not supported in LINQ to Entities.

I know that if I materialize the query it will work fine but I don’t want to do that. I want to do it at the db level, so what’s the best way of doing it, is it possible? Or I have to do this (u.FirstName + " " + u.LastName) all the time

p.s: I've also tried this: (not working for me)

public static Expression<Func<User, string>> FullName()
{
    return u => u.FirstName + " " + u.LastName;
}

Thank you

CinCout
  • 9,486
  • 12
  • 49
  • 67
Zulander
  • 648
  • 1
  • 10
  • 22
  • if you use the model first approach you can create a UDF http://stackoverflow.com/questions/20131632/calling-a-sql-user-defined-function-in-a-linq-query – Mike Tsayper Jun 09 '16 at 04:50
  • 1
    Expression approach should work. Can you show how you tried to use it? – Kaspars Ozols Jun 09 '16 at 05:03
  • if you fill the firstName and lastname the full name will be calculated when accessing the `FullName` property and your Fullname property is get only, how u set the value to that – Eldho Jun 09 '16 at 05:49
  • What's your db ? Sql Server ? If yes, you could create a `Computed Column` : https://msdn.microsoft.com/en-us/library/ms188300.aspx – Raphaël Althaus Jun 09 '16 at 07:28
  • @KasparsOzols: maybe i am not using it well, since i am not familiar with it, but i've tried this: `FullName = u.FullName` – Zulander Jun 09 '16 at 13:05
  • @Zulander : you need to replace the parameter to use your expression in a query. Take a look, for example at http://stackoverflow.com/questions/11159697/replace-parameter-in-lambda-expression – Raphaël Althaus Jun 10 '16 at 08:12

1 Answers1

1

It won't work since db don't know how to execute your server-side method. Possible options:

Translation library:

private static readonly CompiledExpression<Customer, string> fullNameExpression
   = DefaultTranslationOf<User>.Property(e => e.FullName)
                .Is(e => e.FirstName + " " + e.LastName);

[NotMapped]
public string FullName
{
     get { return fullNameExpression.Evaluate(this); }
}

var q = dbContext.Users.Select(u => new
     {
         FullName = u.FullName
     }).WithTranslations(); 

DelegateDecompiler library:

[NotMapped]
[Computed]
public string FullName
{
     get { return FirstName + " " + LastName; }
}


var q = dbContext.Users.Select(u => new
     {
         FullName = u.FullName
     }).Decompile(); 

Encapsulation:

public static Expression<Func<MyEntity, MyDto>> SelectFullNames()
{
      return e => new MyDto{} { Fullname = e.FirstName + " " + e.LastName; 
}

var queryable = dbConext.Users.Select(SelectFullNames());

Disclaimer - libraries no used in real-world-scenarions by me.

pwas
  • 3,225
  • 18
  • 40