1

我有一个看起来像这样的课程:

public class Competitor
{
    public virtual int CompetitorId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }

    public virtual IEnumerable<string> SportsCompeted { get; set; }
}

SportsCompeted是这样解析的 SportID(字符串)列表:

SELECT DISTINCT SportID FROM results WHERE competitorId = xxx

我将如何映射这样的东西?
查看 HasMany 我可以指定一个Where子句,但我认为这不是我在这种情况下要寻找的吗?

我正在使用 Fluent Mappings,为简洁起见省略。

4

2 回答 2

1

你应该能够做到这一点.Element()。就像是:

HasMany(x => x.SportsCompeted)
    .KeyColumn("CompetitorId")
    .Element("SportID") // You can define element type as second parameter
    .Table("results");

更多信息:
使用 NHibernate
Fluent NHIbernate 自动映射 List<string>? 映射字符串集合?


编辑:

假设你有你的ResultSport实体:

public class Sport
{
    public virtual int SportId { get; set; }
    // Other properties
}

public class Result : Entity
{
    public virtual ResultId { get; set; }
    public virtual Competitor Competitor { get; set; }
    public virtual Sport Sport { get; set; }
    // Other properties
}

public class Competitor
{
    public virtual int CompetitorId { get; set; }
    public virtual IList<Result> Results { get; set; }
    // Other properties
}

HasMany现在看起来像这样:

// For this, you would need to have Result and Sport classes mapped
// This property isn't necessary for your Sports Competed query
HasMany(x => x.Results)
    .KeyColumn("CompetitorId")
    .Table("results");

然后你可以使用ie。Linq 得到你想要的结果:

var sports = session.Query<Result>()
    .Where(x => x.Competitor.CompetitorId = competitorId)
    .Select(x => x.Sport) // Or .Select(x => x.Sport.SportId)
    .Distinct();
于 2012-07-02T16:37:08.160 回答
0

我最终做的是在 SQL 中创建一个视图来基本上做:

SELECT DISTINCT SportID, CompetitorID FROM results

然后,在我的映射中:

HasMany(x => x.CompetitorDisciplines)
    .Not.LazyLoad()
    .Inverse()
    .AsBag()
    .KeyColumn("competitorId")
    .Element("DisciplineCode")
    .Table("vCompetitorDisciplines");

这似乎产生了预期的结果。
慢,但它只是一次(或每天一次)操作....

于 2012-07-03T15:37:31.393 回答