-1

我正在使用实体框架(数据库 1st),并且在将实体转换为 linq 到实体查询的一部分时遇到了一些问题,对此非常新,因此我们将不胜感激。

基本上,我有 2 个表,'tblContactTypes' 和 'tblContactDetails',如果我返回一组联系人详细信息,如何为每个表返回一个联系人类型?我可以返回 tblContactType 类的实例,但我不确定如何返回 Contact Type 对象。

我的课程如下(我要整理的行是 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()
    {

    }
}
4

1 回答 1

1

您需要手动构建您的 ContactTypeVO 对象实例。x.tblContactType 是 tblContactType 类型的实例,它不会神奇地转换为 ContactTypeVO 的实例。

我的建议是要么实现一个 ContactTypeVO 构造函数,它接受一个 tblContactType 实例并适当地设置属性,要么实现一个隐式转换运算符来做同样的事情。

构造方法示例:

public ContactTypeVO( tblContactType object )
{
    ContactTypeId = object.ContactTypeId;
    <etc> = object.<etc>
}

然后将 ContactType = x.tblContactType 更改为 ContactType = new ContactTypeVO( x.tblContactType )

于 2013-03-25T21:55:10.157 回答