基于子对象ID的实体框架不同

时间:2019-10-01 15:29:57

标签: c# entity-framework entity-framework-6

我有一个这样的对象

public class GiftBasket
{
        //-----------------------------------------
        [JsonProperty(PropertyName = "Id")]
        public string Id { get; set; }
        [JsonProperty(PropertyName = "Deleted")]
        public bool Deleted { get; set; }
        [JsonProperty(PropertyName = "CreatedAt")]
        public DateTimeOffset CreatedAt { get; set; }
        [JsonProperty(PropertyName = "UpdatedAt")]
        public DateTimeOffset UpdatedAt { get; set; }
        [Version]
        public string Version { get; set; }
        //-----------------------------------------
        [JsonProperty(PropertyName = "UserId")]
        public string UserId { get; set; }
        [JsonProperty(PropertyName = "Source")]
        public string Source { get; set; }
        [JsonProperty(PropertyName = "IsRedeemed")]
        public bool IsRedeemed { get; set; }
        [JsonProperty(PropertyName = "GiftId")]
        public string GiftId { get; set; }
        public Gift gift { get; set; }
// this is the child object 
        [JsonProperty(PropertyName = "MerchantId")]
        public string MerchantId { get; set; }
        public Merchant Merchant { get; set; }
}

我有一个来自数据库的类型为GiftBasket的可观察集合:

private ObservableCollection<GiftBasket> GiftBaskets = new ObservableCollection<GiftBasket>();
GiftBaskets = await manager.GetGiftBasketsByUserIdAsync(Settings.UserId,false);

然后我要像这样选择不同的商人,但是它不能工作,因为该列表将选择GiftBasket中的所有商人,但并没有区别,因为它将在礼品篮中包含所有商人,但是我只想返回唯一身份没有重复的值。

Merchant Merchants = GiftBaskets.Select(x =>x.Merchant).Distinct();

但是,这很好用,但是它只返回我想要整个对象的位置的ID。(我使用GUID作为ID,因此使用string)

String Merchants = GiftBaskets.Select(x =>x.MerchantId).Distinct();

0 个答案:

没有答案