私はエンティティフレームワーク(データベース1番目)を使用していますが、統合言語クエリの一部としてエンティティをキャストする際にいくつかの問題が発生しています。これは非常に新しいため、助けていただければ幸いです。
基本的に、「tblContactTypes」と「tblContactDetails」の2つのテーブルがあります。連絡先の詳細のセットを返す場合、それぞれの連絡先タイプを返すにはどうすればよいですか。tblContactTypeクラスのインスタンスを返すことはできますが、ContactTypeオブジェクトを返す方法がわかりません。
私のクラスは次のとおりです(私が整理しようとしている行は、GetAll()メソッドの'ContactType = x.tblContactType'です)-
データ層-
public partial class tblContactType
{
public tblContactType()
{
this.tblContactDetails = new HashSet<tblContactDetail>();
}
public int ContactTypeID { get; set; }
public string Name { get; set; }
public string ExpressionValidator { get; set; }
public virtual ICollection<tblContactDetail> tblContactDetails { get; set; }
}
public partial class tblContactDetail
{
public tblContactDetail()
{
this.tblCustomerContactDetails = new HashSet<tblCustomerContactDetail>();
}
public int ContactDetailID { get; set; }
public string ContactDetail { get; set; }
public int ContactTypeID { get; set; }
public virtual tblContactType tblContactType { get; set; }
public virtual ICollection<tblCustomerContactDetail> tblCustomerContactDetails { get; set; }
}
ビジネスレイヤー-
public class ContactTypeVO
{
public int ContactTypeID { get; set; }
public string Name { get; set; }
public string ExpressionValidator { get; set; }
/// <summary>
/// Determines whether this instance is new.
/// </summary>
/// <returns>
/// <c>true</c> if this instance is new; otherwise, <c>false</c>.
/// </returns>
public bool IsNew()
{
return ContactTypeID == 0;
}
}
public class ContactDetailVO
{
public int ContactDetailID { get; set; }
public string ContactDetail { get; set; }
public int ContactTypeID { get; set; }
public tblContactType ContactType { get; set; }
public ICollection<tblCustomerContactDetail> CustomerContactDetails { get; set; }
/// <summary>
/// Determines whether this instance is new.
/// </summary>
/// <returns>
/// <c>true</c> if this instance is new; otherwise, <c>false</c>.
/// </returns>
public bool IsNew()
{
return ContactDetailID == 0;
}
}
public class ContactDetailDAO : IDisposable
{
/// <summary>
/// The repository
/// </summary>
public IContactDetailsRepository repository;
/// <summary>
/// Initializes a new instance of the <see cref="ContactDetailEO" /> class.
/// </summary>
public ContactDetailDAO()
{
repository = new ContactDetailsRepository();
}
/// <summary>
/// Saves the specified contact detail.
/// </summary>
/// <param name="contactDetail">The contact detail.</param>
public void Save(ContactDetailVO contactDetail)
{
if (this.IsValid())
{
if (contactDetail.IsNew())
{
repository.Add(new tblContactDetail
{
ContactDetailID = contactDetail.ContactDetailID,
ContactDetail = contactDetail.ContactDetail,
ContactTypeID = contactDetail.ContactTypeID
});
}
else
{
var saveItem = repository.AsQueryable().Where(x => x.ContactDetailID == contactDetail.ContactDetailID).FirstOrDefault();
if (saveItem != null)
{
saveItem.ContactDetailID = contactDetail.ContactDetailID;
saveItem.ContactDetail = contactDetail.ContactDetail;
saveItem.ContactTypeID = contactDetail.ContactTypeID;
repository.Update(saveItem);
}
}
}
else
{
// ERROR
}
}
/// <summary>
/// Gets all.
/// </summary>
/// <returns></returns>
public List<ContactDetailVO> GetAll()
{
var contacts = repository.AsQueryable()
.Select(x => new ContactDetailVO
{
ContactDetailID = x.ContactDetailID,
ContactDetail = x.ContactDetail,
ContactTypeID = x.ContactTypeID,
ContactType = x.tblContactType,
});
return contacts.ToList();
}
/// <summary>
/// Gets the by ID.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
public ContactDetailVO GetByID(int id)
{
var contactDetail = repository.AsQueryable().Where(x => x.ContactDetailID == id).FirstOrDefault();
if (contactDetail == null)
return null;
return new ContactDetailVO
{
ContactDetailID = contactDetail.ContactDetailID,
ContactDetail = contactDetail.ContactDetail,
ContactTypeID = contactDetail.ContactTypeID,
ContactType = contactDetail.tblContactType
};
}
/// <summary>
/// Determines whether this instance is valid.
/// </summary>
public bool IsValid()
{
return true;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
}
}