簡體   English   中英

LINQ獲取列表中的項目<AttributeValuePair>

[英]LINQ get items in List<AttributeValuePair>

我的數據庫上有一張表,除了其他列(其中一列是UniqueIdentifier)外,我還有一列有一個JSON數組字符串,其值如下(格式):

[
    {
        "AttributeId": "fe153d69-8ac1-6e0c-8793-ff0000804eb3",
        "AttributeValueId": "64163d69-8ac1-6e0c-8793-ff0000804eb3"
    },
    {
        "AttributeId": "00163d69-8ac1-6e0c-8793-ff0000804eb3",
        "AttributeValueId": "67163d69-8ac1-6e0c-8793-ff0000804eb3"
    }
]

然后,我有了這個AttributeValuePair類,它使我可以通過代碼讀取以下數據:

public class AttributeValuePair
{
    public AttributeValuePair();

    public Guid AttributeId { get; set; }
    public Guid AttributeValueId { get; set; }
}

每當我從該表中獲得項目列表時,我都希望能夠僅基於一個AttributeValueId過濾結果數組,並僅獲得匹配的項目,而與其他任何屬性的值無關。

從代碼上看,要讀取這些屬性集合,我必須具有List<AttributeValuePair> ,如何在LINQ中獲取存在特定AttributeValueId的項?

List<AttributeValuePair> attributeValuePairs = serializer.Deserialize<List<AttributeValuePair>>(item.Variant);

我已經迷失了兩個小時,似乎無法從中逃脫。

編輯

為了更清楚地了解這個問題,我想做的是從List<ProductVariation> ,當屬性“ Days”為指定值時,獲取屬性“ Portions”的可能值。 使用serializer器構建LINQ語句時遇到很多麻煩。

//This code is wrong, I know, but I'm trying to show what I want
result = model.ProductVariations.Find(x, new {serializer.Deserialize<List<AttributeValuePair>>(item.Variant).Where(valuePair => valuePair.AttributeId == attributeId)});

你能試一下嗎

attributeValuePairs.Where(valuePair => valuePair.AttributeId == new Guid("SomeValue"));

這個問題的答案實際上比以前預期的要簡單得多:

public string SelectedVariation(string mealsAttribute, string portionsAttribute, string product)
{
    Guid productId = new Guid(product);
    CatalogManager catalogManager = CatalogManager.GetManager();
    EcommerceManager ecommerceManager = EcommerceManager.GetManager();

    RegisterOrderAccountFormModel model = new RegisterOrderAccountFormModel();
    model.Product = catalogManager.GetProduct(productId);
    List<ProductVariation> productVariationsCollection = catalogManager.GetProductVariations(productId).ToList();

    //This is the really interesting part for the answer:
    return productVariationsCollection.Where(x => x.Variant.ToLower().Contains(mealsAttribute.ToLower()) && x.Variant.ToLower().Contains(portionsAttribute.ToLower())).FirstOrDefault().Id.ToString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM