C#从两个类中检索数据

时间:2015-06-23 08:12:40

标签: c#

这是我的简单课程:

public class Location
{
    public int id { get; set; }
    public Single x { get; set; }
    public Single y { get; set; }
    public Single z { get; set; }
    public float Distance { get; set; }
}

我正在访问它,如下所示:

public static List<Profile> Loc = ParseXml(); 
var SortedList = Loc.OrderBy(o => o.Distance).ToList();

现在,XML结构发生了变化,我修改了我的类,如下所示:

public class Profile 
{
    private List<Location> loc = new List<Location>();
    public int Current { get; set; }
    public List<Location> Location { get; set; }
}

public class Location
{
    public int id { get; set; }
    public Single x { get; set; }
    public Single y { get; set; }
    public Single z { get; set; }
    public float Distance { get; set; }
}

现在,我想要检索Location类中的元素,该元素在ID类的Current最低Profile之下具有最低->add('brand') ->add('model') ->add('price') ->add('image', 'vich_image', array( 'required' => false, 'allow_delete' => true, // not mandatory, default is true 'download_link' => true, // not mandatory, default is true )); ->add('updatedAt') ->add('sub_rel')

1 个答案:

答案 0 :(得分:1)

要将private List<Location> loc = new List<Location>()用于Location属性,您需要将其更改为非自动语法:

public List<Location> Location { get { return loc; } }

请注意,我删除了设置器。

要使Current属性返回最近的位置,您还需要在属性中使用实际代码:

public int Current 
{
   get { Loc.OrderBy(o => o.Distance).First().id; }
} 

请注意,如果列表为空,这将给出异常!也没有必要设置&#34;当前价值。