0

我找到了这个通用存储库,但我确实找到了一种使用它的方法:

    public T FindSingle(Expression<Func<T, bool>> predicate = null, params Expression<Func<T, object>>[] includes)
    {
        var set = FindIncluding(includes);

        return (predicate == null) ? set.FirstOrDefault() : set.FirstOrDefault(predicate);
    }

我怎么能称之为包括这样的几个相关实体?:

        Message messageParentDomain = _messageRepository.FindSingle(m => m.Id == messageDto.MessageParent_Id, includes: i => i.MemberFrom, i => i.MemberTo);  

留言类:

public class Message
{
    // Primary properties
    public int Id { get; set; }
    public string NameFrom { get; set; }
    public string EmailFrom { get; set; }
    public string TelephoneFrom { get; set; }
    public string Title { get; set; }
    public string MessageText { get; set; }
    public bool? Approved { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime? DateRead { get; set; }

    // Navigation properties
    public Member MemberFrom { get; set; }
    public Member MemberTo { get; set; }
    public MessageType MessageType { get; set; }
    public Message MessageParent { get; set; }

    public Ad Ad { get; set; }
    public Ask Ask { get; set; }
}

谢谢。

4

1 回答 1

0

这是编译的方法的一个版本:

public T FindSingle<T>(Expression<Func<T, bool>> predicate = null, params Expression<Func<T, object>>[] includes)
{
    var set = FindIncluding(includes);

    return (predicate == null) ? set.FirstOrDefault() : set.FirstOrDefault(predicate);
}

我假设 T 被调用Message(你不能通过 T)给我们FindSingle<Message>and Expression<Func<Message, object>>。您需要将 T 替换为您真正使用的对象。试试这段代码...

Message messageParentDomain = 
    _messageRepository.FindSingle<Message>(
        m => m.Id == messageDto.MessageParent_Id, 
        includes: new Expression<Func<Message, object>>[] {
            i => i.MemberFrom,
            i => i.MemberTo
        }); 
于 2013-03-14T15:55:06.313 回答