C #, LINQ, SQL: compound columns

I have a LINQ object for which I need to create a special string value. These are 7 different values, separated by a "-". Some of the fields used are in this table, and some of them are in different tables. I would like to add this field to the object one way or another so that I do not have to create this line every time I need it.

I thought I could add it to an entity with a partial class like this:

    public string SpecialField
    {
        get
        {
            return string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}",
                                 TableId,
                                 Number,
                                 ForeignTableA.Date,
                                 ForeignTableB.Name,
                                 ForeignTableC.ForeignTableD.Name,
                                 ForeignTableB.ForeignTableE.Name,
                                 ForeignTableB.Number ?? 0);
        }
    }

However, after writing this, I got a little angry at how this would work. The reason, if I am mistaken, this will result in a database query every time this value is used for each item. And will this work, for example, to use this field in a Where clause?

Where, , , , .

?

+3
3

. SQL Server ,

+2

, let . , - :

var query = from master in MasterTable
            join foreignA in ForeignTableA on ...
            join foreignB in ForeignTableB on ...
            let special = string.Format ("...", master.TableID, ...)
            where special.Contains ("foo")
            select { 
                 // ...
                 string specialResult = special,
                 // ...
            }
+1

, ? , , . (, , SpecialField .)

protected string specialField = null;

public string SpecialField
{
    get
    {
        if (specialField == null)
        {
            specialField = string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}",
                             TableId,
                             Number,
                             ForeignTableA.Date,
                             ForeignTableB.Name,
                             ForeignTableC.ForeignTableD.Name,
                             ForeignTableB.ForeignTableE.Name,
                             ForeignTableB.Number ?? 0);
        }
        return specialField;
    }
}
0

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


All Articles