如何找到每个地方所属的 Parents 名称

How do I find the Parents name of each place belongs

public class ZipListViewModel 
{
    /// <summary>HIERARCHY_CODE</summary>
    public string HIERARCHY_CODE{ get; set;}

    /// <summary>prent_zipcode/summary>
    public string Zip_1{ get; set;}

    /// <summary> parent_name /summary>
    public string ZipName_1 { get; set;}

    /// <summary>place zipcode /summary>
    public string Zip_2 { get; set;}

    /// <summary>LOCATION</summary>
    public string LOCATION { get; set;}
}


var db = new Entities(); 
var source = from a in db.ZIP
select new ViewModels.ZipListViewModel
{
    ID = a.ID,
    HIERARCHY_CODE = a.HIERARCHY_CODE,
    Zip_1 = a.ZIP_1,
    Zip_2 = a.ZIP_2,
    LOCATION = a.LOCATION
};

Zip_1Zip_2 parent 的邮政编码。 HIERARCHY_CODE{ 0, 1 , 2 } ,对于每个城市的层级

我去看了这个节目ZipListViewModel像这样

  ID = a.ID,
  HIERARCHY_CODE = a.HIERARCHY_CODE,
  Zip_1 = a.ZIP_1,
  *ZipName_1 =a.ZipName_1*,
  Zip_2 = a.ZIP_2,
  LOCATION =a.LOCATION

我该怎么做? 我解释的更清楚。 像这样的数据库数据

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1" cellpadding="1" cellspacing="1" style="width: 200px;">
    <tbody>
        <tr>
            <td>
                HIERARCHY_CODE</td>
            <td>
                Zip_1</td>
            <td>
                Zip_2</td>
            <td>
                Location</td>
        </tr>
        <tr>
            <td>
                0</td>
            <td>
                480</td>
            <td>
                480</td>
            <td>
                &nbsp;Arizona</td>
        </tr>
        <tr>
            <td>
                1</td>
            <td>
                &nbsp;480&nbsp;</td>
            <td>
                85048&nbsp;</td>
            <td>
                Phoenix</td>
        </tr>
    </tbody>
</table>

如何在这个 VeiwModel 中显示 Phoenix 属于 Arizona, 喜欢 Zip1_name='Arizona'

我建议您将 ParentLocation string 属性 添加到您的 ZipListViewModel,然后存储在您需要的字段位置 string

如果你的 BD 模型中没有关系,你可以这样做:

var db = new Entities();
var source = db.ZIP.Select(x => new ViewModels.ZipListViewModel
    {
        ID = x.ID,
        HIERARCHY_CODE = x.HIERARCHY_CODE,
        Zip_1 = x.ZIP_1,
        Zip_2 = x.ZIP_2,
        LOCATION = x.LOCATION,
        ParentLocation = !String.IsNullOrWhiteSpace(x.Zip_1) ? db.ZIP.FirstOrDefault(y => y.Zip_2 == x.Zip_1).LOCATION : String.Empty
    });

这是扩展方法语法。