从元组中带回不同的值

时间:2015-08-18 16:45:11

标签: c# asp.net list generics tuples

您好我正在使用从元组到列表的动态数据填充菜单。我遇到了带来不同价值观的问题。而不是我的元组带回3件物品,因为它应该带回来它将物品带回8次。我不确定我做错了什么?我正在使用C#。我认为问题可能在下面的代码行中:

var attributeByCategory = customAttributes.Select(a => a.Item2).Where(b => b.Attribute.CategoryId == categoryTuple.AttributeCategoryId).Distinct();

我在下面发布我的其余代码。如果有人能帮助我,这将是非常有帮助的。我是使用泛型的新手。

protected void createFilter(int categoryid)
{
    // check cateogyrid 

    //get list of proudct id
    List<int> productIds = new List<int>();
    DataRow[] productRow = CategoriesProductsData.Tables["Products"].Select("Category_ID = " + 573);

    productIds = productRow.Select(p => int.Parse(p["Product_ID"].ToString())).ToList();

    //get attributes
    ITCProductService pService = new TCProductServiceClient();
    var productTuples = (pService.GetProductsAttributes(productIds));

    List<Tuple<int, CustomAttribute>> customAttributes = new List<Tuple<int, CustomAttribute>>();
    foreach (var productTuple in productTuples)
    {
        foreach (var attributeTuple in productTuple.m_Item2)
        {
            var customAttribute = new Tuple<int, CustomAttribute>(productTuple.m_Item1, new CustomAttribute(attributeTuple));
            customAttributes.Add(customAttribute);
        }
    }

    List<CustomAttributeCategory> categories = new List<CustomAttributeCategory>();

    var categoryTuples = customAttributes.Select(a => a.Item2).Select(a => a.Attribute.Category).Distinct();

    foreach (var categoryTuple in categoryTuples)
    {
        var category = new CustomAttributeCategory(categoryTuple);

        var attributeByCategory = customAttributes.Select(a => a.Item2).Where(b => b.Attribute.CategoryId == categoryTuple.AttributeCategoryId).Distinct();
        foreach (var attributeTuple in attributeByCategory)
        {
            var attribute = new CustomAttribute(attributeTuple.Attribute);
            var attributeProductIds = customAttributes.Where(a => a.Item2.Attribute.AttributeId == attributeTuple.Attribute.AttributeId).Select(a => a.Item1).ToList();
            attribute.ProductIds = attributeProductIds;


            category.Attributes.Add(attribute);
        }
        categories.Add(category);            

      }


    foreach (var cat in categories)
    {
        var itemCategory = new RadMenuItem(cat.Category.Name.ToString());
        handsetMenu.Items.Add(itemCategory);

        foreach (var attr in cat.Attributes)
        {
            itemCategory.Items.Add(new RadMenuItem(attr.Attribute.Value));
        }
    }
  }

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

不相关但尝试重构这样的方法会让它更具可读性我会试着找出你的问题:

List<Tuple<int, CustomAttribute>> customAttributes = GetCustomAttrubutes(productTuples)

protected List<Tuple<int, CustomAttribute>> GetCustomAttrubutes([ProductTuplesType] productTuples)
{
    foreach (var productTuple in productTuples)
    {
       foreach (var attributeTuple in productTuple.m_Item2)
        {
            var customAttribute = new Tuple<int, CustomAttribute>(productTuple.m_Item1, new CustomAttribute(attributeTuple));
            customAttributes.Add(customAttribute);
        }
    }
}

修改

您的代码中有很多内容,并且使用我只能猜测的数据。

您在整个元组上做的不同而不仅仅是CategoryId,您可能想要使用MoreLinq中的distinctby(b => b.Attribute.CategoryId)

修改       为了帮助理解这一点,其他开发人员理解这一点,你应该转而使用正确的模型,即

public class Products {
   public int id;
   public List<Attributes> Atrributes
}

public class Attributes {
   public int id;
   public string Name;
}

这在调试和提高可读性时更有意义