C# C计算总重量

C# C计算总重量,c#,calculation,C#,Calculation,我有两门课,如下所示。我会得到产品列表“ProductBooleans”以及类别标志(如果它们适合的话)。我还得到了每个类别的产品重量。我需要找到每个类别的总重量 public class ProductBooleans { public int Prodid; public int Cat1; public int Cat2; public int Cat3; } public class ProductWeight { public string C

我有两门课,如下所示。我会得到产品列表“ProductBooleans”以及类别标志(如果它们适合的话)。我还得到了每个类别的产品重量。我需要找到每个类别的总重量

public class ProductBooleans
{
    public int Prodid;
    public int Cat1;
    public int Cat2;
    public int Cat3;
}

public class ProductWeight
{
    public string Cat;
    public int Wt;
}
我需要计算篮子中每个产品的总重量,并返回dict

注意:我目前正在使用反射来解决这个问题,但我认为必须有更简单的方法来实现这一点。
有什么方法可以做到这一点还是更简单的方法?

这似乎可以帮我完成这项工作:

var query =
    from pb in pbs
    let totalWt =
    (
        from x in new (string Cat, int Wt)[]
        {
            ("Cat1", pb.Cat1),
            ("Cat2", pb.Cat2),
            ("Cat3", pb.Cat3),
        }
        join pw in pws on x.Cat equals pw.Cat
        select x.Wt * pw.Wt
    ).Sum()
    select new
    {
        pb.Prodid,
        totalWt
    };
这给了我:

下面是一个使用反射的版本:

(string Cat, int Wt)[] GetValues(ProductBooleans pb) =>
    typeof(ProductBooleans)
        .GetFields()
        .Where(p => p.Name.StartsWith("Cat"))
        .Where(p => p.FieldType == typeof(int))
        .Select(p => (p.Name, (int)p.GetValue(pbs[0])))
        .ToArray();

var query =
    from pb in pbs
    let totalWt =
    (
        from x in GetValues(pb)
        join pw in pws on x.Cat equals pw.Cat
        select x.Wt * pw.Wt
    ).Sum()
    select new
    {
        pb.Prodid,
        totalWt
    };

显然,如果需要的话,可以将.GetFields更改为.GetProperties。

是否会有不同的类首先引导您使用反射?只要类保持这种状态,Linq似乎是可行的。请更新你的帖子:Q:你从哪里读取数据?文本文件?问:你能给我们看一些代码吗?附言:我无法想象你是如何——或者为什么——使用反射的。我希望你的代码能帮助澄清…@paulsm4-我正在读db的文章。类似于以下内容的matchingCriteria.GetType.GetPropertypropertyName.GetValuematchingCriteria,null;谢谢你的回答。总的类别可能会有所不同……所以我需要有一个通用的方法。@user3906850-你需要在你的问题中包含这个要点。@user3906850-我添加了一个使用反射的答案。我很感兴趣的是为什么你有一个需要使用反射的对象模型。我不想实际使用反射。我在代码中计算产品的匹配条件,并在db WeightRules表中为每个条件设置权重。我可以为不同的策略设置不同的权重,每个产品都将根据权重规则计算权重。@user3906850-您的问题很快变得越来越不符合您的实际需要。你需要确保你所问的是你所需要的。你能提供清晰详细的例子来计算我的产品的匹配标准和不同策略的不同权重集,每个产品都会根据权重规则来计算权重吗?为什么你要更改类定义,而不将它们与代码一起发布?我们如何测试这一点?
        var _categoryDic = new Dictionary<Category, bool>();
        _categoryDic.Add(new Category() { CategoryName = "cate1", CatId = 1 }, true);
        _categoryDic.Add(new Category() { CategoryName = "cate2", CatId = 2 }, false);
        _categoryDic.Add(new Category() { CategoryName = "cate3", CatId = 3 }, true);
        productBooleans.Add(new ProductBooleans()
        {
            prodid = 333
                        ,
            categoryDic = _categoryDic
        });

        var _categoryDic2 = new Dictionary<Category, bool>();
        _categoryDic2.Add(new Category() { CategoryName = "cate1", CatId = 1 }, true);
        _categoryDic2.Add(new Category() { CategoryName = "cate2", CatId = 2 }, true);
        _categoryDic2.Add(new Category() { CategoryName = "cate3", CatId = 3 }, false);
        productBooleans.Add(new ProductBooleans()
        {
            prodid = 444
                        ,
            categoryDic = _categoryDic2
        });

        List<ProductWeight> productWeights = new List<ProductWeight>();

        productWeights.Add(new ProductWeight() { category = new Category() { CategoryName = "cate1", CatId = 1 }, weight = 10 });
        productWeights.Add(new ProductWeight() { category = new Category() { CategoryName = "cate2", CatId = 2 }, weight = 20 });
        productWeights.Add(new ProductWeight() { category = new Category() { CategoryName = "cate3", CatId = 3 }, weight = 30 });

        var _prodBool =productBooleans.Select(i => new {
                                                                    prod_id = i.prodid
                                                                     ,
            categoryList = i.categoryDic
                            .Where(j => j.Value)
                            .Select(j => j.Key)
                            .ToList<Category>()
        }).ToList();
        Dictionary<int, int> resultList=new Dictionary<int, int>();
        foreach (var item in _prodBool)
        {
            int itemweight = 0;
            foreach (var pw in productWeights)
            {
                itemweight += (item.categoryList.Where(i => i.CatId == pw.category.CatId).Any()) ? pw.weight : 0;
            }
            resultList.Add(item.prod_id, itemweight);
        }
        return resultList;
(string Cat, int Wt)[] GetValues(ProductBooleans pb) =>
    typeof(ProductBooleans)
        .GetFields()
        .Where(p => p.Name.StartsWith("Cat"))
        .Where(p => p.FieldType == typeof(int))
        .Select(p => (p.Name, (int)p.GetValue(pbs[0])))
        .ToArray();

var query =
    from pb in pbs
    let totalWt =
    (
        from x in GetValues(pb)
        join pw in pws on x.Cat equals pw.Cat
        select x.Wt * pw.Wt
    ).Sum()
    select new
    {
        pb.Prodid,
        totalWt
    };
        var _categoryDic = new Dictionary<Category, bool>();
        _categoryDic.Add(new Category() { CategoryName = "cate1", CatId = 1 }, true);
        _categoryDic.Add(new Category() { CategoryName = "cate2", CatId = 2 }, false);
        _categoryDic.Add(new Category() { CategoryName = "cate3", CatId = 3 }, true);
        productBooleans.Add(new ProductBooleans()
        {
            prodid = 333
                        ,
            categoryDic = _categoryDic
        });

        var _categoryDic2 = new Dictionary<Category, bool>();
        _categoryDic2.Add(new Category() { CategoryName = "cate1", CatId = 1 }, true);
        _categoryDic2.Add(new Category() { CategoryName = "cate2", CatId = 2 }, true);
        _categoryDic2.Add(new Category() { CategoryName = "cate3", CatId = 3 }, false);
        productBooleans.Add(new ProductBooleans()
        {
            prodid = 444
                        ,
            categoryDic = _categoryDic2
        });

        List<ProductWeight> productWeights = new List<ProductWeight>();

        productWeights.Add(new ProductWeight() { category = new Category() { CategoryName = "cate1", CatId = 1 }, weight = 10 });
        productWeights.Add(new ProductWeight() { category = new Category() { CategoryName = "cate2", CatId = 2 }, weight = 20 });
        productWeights.Add(new ProductWeight() { category = new Category() { CategoryName = "cate3", CatId = 3 }, weight = 30 });

        var _prodBool =productBooleans.Select(i => new {
                                                                    prod_id = i.prodid
                                                                     ,
            categoryList = i.categoryDic
                            .Where(j => j.Value)
                            .Select(j => j.Key)
                            .ToList<Category>()
        }).ToList();
        Dictionary<int, int> resultList=new Dictionary<int, int>();
        foreach (var item in _prodBool)
        {
            int itemweight = 0;
            foreach (var pw in productWeights)
            {
                itemweight += (item.categoryList.Where(i => i.CatId == pw.category.CatId).Any()) ? pw.weight : 0;
            }
            resultList.Add(item.prod_id, itemweight);
        }
        return resultList;