1

関連フィールド (外部キー CountryId) を持つ 2 つのビジネス オブジェクトがある場合、従業員クラスのリストを表示するときに、CountryId の代わりに CountryName を表示するにはどうすればよいですか?

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string FamilyName { get; set; }
    public int CountryId { get; set; }
}

public class Country
{
    public int Id { get; set; }
    public string CountryName { get; set; }
}
4

2 に答える 2

0

従業員のリストとすべての国のリストがある場合は、LINQを使用してそれらを結合し、必要なプロパティを使用して匿名オブジェクトを作成できます。

List<Employee> employees = // Get list of all employees
List<Country> countries = // Get list of all countries

var employeesWithCountryName = 
from e in employees
join c in countries on e.CountryID = c.Id
select new { 
  Id = e.Id, 
  FirstName = e.FirstName, 
  FamilyName = e.FamilyName,
  CountryId = e.CountryId, 
  CountryName = c.CountryName
}
于 2009-08-14T10:28:03.590 に答える
0

ORM を使用している場合、ほとんどの場合、関係をナビゲートして、国の名前やその他の国のプロパティを取得できます。

データベース用語では、結合が必要です。

于 2009-11-11T09:05:26.070 に答える