# Calculated fields on the server side

**URL:** <https://forum.radzen.com/t/calculated-fields-on-the-server-side/403>\
**Category:** Uncategorized\
**Created:** [July 5, 2018, 8:19pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403 "2018-07-05T20:19:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![semafox](https://forum.radzen.com/user_avatar/forum.radzen.com/semafox/32/55_2.png) [@semafox](https://forum.radzen.com/u/semafox)\
**Post date:** [July 5, 2018, 8:19pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/1 "2018-07-05T20:19:10Z")

</div>

I would like to add a calculated field to a “Person” table, for example a Fullname field which would be derived from concatenating Firstname + " " + Lastname. (I realize this could probably also be done on the client side in this case, but other more complex scenarios would require a server side computation)

Can I create a Person.Custom.cs partial class with the new Fullname property?

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [July 6, 2018, 8:21am UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/2 "2018-07-06T08:21:56Z")

</div>

Hi @semafox,

Yes, you can create a partial class and add the new property. However there are a few other things required. Here is the full procedure of adding a FullName property to the Employee entity from the Northwind database (that already has FirstName and LastName properties).

1. Create a partial class in the server\models\Northwind\ directory and add the property (the empty setter is needed):

```auto
    public partial class Employee
    {

        public string FullName
        {
            get
            {
                return this.FirstName + " " + this.LastName;
            }

            set
            {
            }
        }
    }

```

1. Create a new partial class that will extend the EF context in order to tell it to ignore that calculated property:

```auto
    public partial class NorthwindContext
    {
        partial void OnModelBuilding(ModelBuilder builder)
        {
            builder.Entity<Employee>().Ignore(e => e.FullName);
        }
    }

```

1. Finally use that property in Radzen. It won't display in autocomplete and dropdowns so you have to type it in:

 ![04](https://forum.radzen.com/uploads/default/original/1X/62f3c5a784e22aaca1c33ba42d975c0b1aa3c97d.png)

In the end the computed property should display in a DataGrid like this:

 ![25](https://forum.radzen.com/uploads/default/original/1X/a170276db569ba999ec4985d1df1c2e1ce942f45.png)

---

<div class="post-metadata">

**Author:** ![semafox](https://forum.radzen.com/user_avatar/forum.radzen.com/semafox/32/55_2.png) [@semafox](https://forum.radzen.com/u/semafox)\
**Post date:** [July 6, 2018, 12:55pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/3 "2018-07-06T12:55:36Z")

</div>

That works great! Very nice!

One question though: filtering on this field works but is case sensitive, how can I change that?

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [July 6, 2018, 3:01pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/4 "2018-07-06T15:01:54Z")

</div>

Can you try the following:

1. Change the property definition to:

```auto
        public string FullName
        {
            get;
            set;
        }

```

1. Create a new partial file for the controller of that entity e.g. EmployeeController with the following method:

```auto
  public partial class EmployeesController
  {
      partial void OnEmployeesRead(ref IQueryable<Employee> items)
      {
          items = items.Select(item => new Employee{
              EmployeeID = item.EmployeeID,
              FirstName = item.FirstName,
              FullName = item.FirstName + " " + item.LastName
          });
      }
  }

```

What this does is to change the place where the field calculation takes place. This one should do it at DB level (in SQL). Then if your SQL collation is set to ignore case filtering should work as expected.

---

<div class="post-metadata">

**Author:** ![danh](https://forum.radzen.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@danh](https://forum.radzen.com/u/danh)\
**Post date:** [March 22, 2021, 6:07pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/5 "2021-03-22T18:07:24Z")

</div>

Will this work on an expanded table? Like if you had Employee expanded in the Orders grid, could you search on FullName from there? I have a similar case and can't get it to work.

---

<div class="post-metadata">

**Author:** ![danh](https://forum.radzen.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@danh](https://forum.radzen.com/u/danh)\
**Post date:** [March 22, 2021, 6:18pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/6 "2021-03-22T18:18:53Z")

</div>

Actually, I can't get the search to work at all, even on the Employees grid. Getting this error in the Network \> Response tab:  
System.InvalidOperationException: The LINQ expression 'DbSet()  
.Where(e =\> e.FullName.Contains(\_\_TypedProperty\_0))' could not be translated. Additional information: Translation of member 'FullName' on entity type 'Employee' failed. This commonly occurs when the specified member is unmapped. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

Seems like it's looking for a field in the database...

---

<div class="post-metadata">

**Author:** ![danh](https://forum.radzen.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@danh](https://forum.radzen.com/u/danh)\
**Post date:** [March 23, 2021, 12:41pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/7 "2021-03-23T12:41:46Z")

</div>

OK, I was able to get it working. I had been incorrectly using a foreach loop in the OnEmployeesRead method instead of the items.Select method shown here. I just figured a foreach loop would do the same thing, but evidentially not. Shows how much I understand C# I guess.

One question for you C# gurus, do I have to name all the members when I add one like this? In TypeScript there is the spread operator that would make something like this very easy.  
Something like:

> ```
> items = items.Select(item => new Employee {
> ...item,
> FullName = item.FirstName + " " + item.LastName
> });
> 
> ```

Is there anything like that in C#?

Thanks, Dan

---

<div class="post-metadata">

**Author:** ![danh](https://forum.radzen.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@danh](https://forum.radzen.com/u/danh)\
**Post date:** [March 25, 2021, 1:52pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/8 "2021-03-25T13:52:55Z")

</div>

With the help of a colleague, I did find a solution to keep from having to innumerate all the properties.  
I used the AutoMapper package and it works great.  
For anyone interested, here is how it works.

1. Run this from the command prompt, in the project `server` directory:  
`dotnet add package AutoMapper`
2. In your Models \> Northwind \> Employee.Custom.cs (you have to Ignore Collections):

> ```
> namespace Northwind.Models.NorthwindDb
> {
> public partial class Employee
> {
> public string FullName { get; set; }
> }
> public class EmployeeProfile: AutoMapper.Profile
> {
> public EmployeeProfile()
> {
> CreateMap<Employee,Employee>()
> .ForMember(dest => dest.Employees1, opt => opt.Ignore())
> .ForMember(dest => dest.Orders, opt => opt.Ignore())
> .ForMember(dest => dest.EmployeeTerritories, opt => opt.Ignore())
> .ForMember(dest => dest.FullName,
> opt => opt.MapFrom(src => src.FirstName + " " + src.LastName)
> );
> }
> }
> }
> 
> ```

1. In Controllers \> NorthwindDB \> EmployeeController.Custom.cs:  
using System.Linq;

> ```
> namespace Northwind.Controllers.NorthwindDb
> {
> using Northwind.Models.NorthwindDb;
> using AutoMapper.QueryableExtensions;
> 
> public partial class EmployeesController
> {
> partial void OnEmployeesRead(ref IQueryable<Employee> items)
> {
> var config = new AutoMapper.MapperConfiguration(cfg => cfg.AddProfile<EmployeeProfile>());
> items = items.ProjectTo<Employee>(config);
> }
> }
> }
> 
> ```

1. Then, to keep the ModelBuilder happy, here is my Data \> NorthwindDBContext.Custom.cs file:

> ```
> using Microsoft.EntityFrameworkCore;
> using Northwind.Models.NorthwindDb;
> namespace Northwind.Data
> {
> public partial class NorthwindDbContext
> {
> partial void OnModelBuilding(ModelBuilder builder)
> {
> builder.Entity<Employee>().Ignore(e => e.FullName);
> }
> }
> }
> 
> ```

I think you could also just use [NotMapped] in the Model definition, instead of the last step. Not sure about that as I didn't try it.  
Thanks, Dan

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [March 25, 2021, 1:54pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/9 "2021-03-25T13:54:16Z")

</div>

Hey @danh,

Please format your code. You can use markdown code blocks.

---

<div class="post-metadata">

**Author:** ![danh](https://forum.radzen.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@danh](https://forum.radzen.com/u/danh)\
**Post date:** [March 25, 2021, 2:07pm UTC](https://forum.radzen.com/t/calculated-fields-on-the-server-side/403/10 "2021-03-25T14:07:31Z")

</div>

OK, I think I got it now.
