如何找到每个地方的父母姓名

时间:2015-04-13 09:09:52

标签: c# asp.net-mvc http

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父级的zipCode。对于每个城市的层次结构,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 属于亚利桑那, 比如Zip1_name='Arizona'

1 个答案:

答案 0 :(得分:0)

我建议您将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
    });

这是扩展方法语法。