1

我有桌子:

Items{ItemId, Title, CreatedBy, CreatedOnDate}
Likes{ItemId, UserId, CreatedOnDate}

在 ef 我有这两个模型:

[Table("Likes")]
    public class Like
    {
        [Key, Column(Order = 1)]
        public int ItemId { get; set; }
        [Key, Column(Order = 2)]
        public Guid UserId{ get; set; }
        public DateTime CreatedOnDate { get; set; }

        public virtual Item Item { get; set; }
        public virtual User User { get; set; }
    }
[Table("Items")]
public class Item
    {
        [Key]
        public int ItemId { get; set; }
        public virtual ICollection<Like> Likes { get; set; }
    }

现在我有一个问题来获取用户喜欢的项目列表。我尝试了以下方法:

model = (from l in con.Likes
                       select new Item
                                  {
                                      UserId = l.Item.UserId,
                                      Title = l.Item.Title,                                      
                                      Likes = l.Item.Likes,
                                      User = l.Items.User,                                      
                                      CreatedOnDate = l.Ticket.CreatedOnDate,
                                      ItemId = l.Ticket.ItemId
                                  }).ToList();

我试过有和没有ToList(). 我在这里做错了什么?

4

1 回答 1

1

...获取用户喜欢的项目列表...

读到这里,我建议:

var query = from l in con.Likes
            where l.User.UserId == givenUserId
            select l.Item;
var result = query.ToList();

但这与您自己的查询相去甚远,以至于我感觉您想要别的东西。

于 2012-07-25T21:15:28.327 回答