Can I combine fields through Linq with Sql

I'm new to Linq to Sql, I would like to create one single read-only property in the Entity class, which is a combination of several fields, in particular, I want to create a name field that combines the title, first name and last name.

I see how to do this by modifying the .designer.cs file, but I know that I should not touch this.

I am trying to do this so that in dynamic data I get a single column, not multiple columns.

+3
source share
2 answers

The trick here is to use partial class; create a new .cs file and in the same namespace add a new class with a name for your type:

namespace My.Data.Namespace {
    partial class Employee {
        public string Foo {
            get {return Forename + " " + Surname; } // etc
        }
    }
}

This is combined with the original (generated) class file. This is a general approach for adding additional functions to generated objects (including data context). You can also look at "partial methods" since a number of private methods are defined and used by LINQ-to-SQL.

One warning: if your dbml is called MySystem.dbml, then avoid creating the file MySystem.cs(presumably for your partial classes); There is an error in the code generator that could lead to a crash. Just call it something else.

+4
source

LINQ, - :

select new {FullName = x.FirstName + " " + x.Surname};

, 100%, .

0

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


All Articles