写在匿名类型分组依据 [英] Writing Group By on Anonymous Types

查看:132
本文介绍了写在匿名类型分组依据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我书面方式GROUP BY子句对其中两个连接起来,并通过实体数据模型被访问的表。我无法遍历匿名类型,有人可以帮我。

 公共字符串GetProductNameByProductId(INT productId参数)
    {
        字符串PRODNAME =的String.Empty;
        使用(VODConnection vodObjectContext =新VODConnection())
        {

            变种产品BP =在vodObjectContext.BFProducts
                                             加入BPF在vodObjectContext.BFProductMasters上bp.ProductMasterId等于bpf.ProductMasterId
                                             其中,bp.ProductId == productId参数
                           BP集团通过新{的ProductId = bp.ProductId,产品名称= bp.ProductName,ProductMasterName = bpf.ProductMasterName}到newInfo
                           选择newInfo;

//要遍历产品或实际上需要获得所有结果。我该怎么办呢?想让productmastername属性在PRODNAME变量通过重复设置

            返程(PRODNAME);
        }
    }
 

解决方案

一个问题是,你已经使用查询延续无缘无故。但这仍然不应该有$ P $使用该属性,你要知道pvented你。试试这个作为一个稍微更简洁的方法:

 变种产品从BP =在vodObjectContext.BFProducts
               加入BPF在vodObjectContext.BFProductMasters
                 在bp.ProductMasterId等于bpf.ProductMasterId
               其中,bp.ProductId == productId参数
               BP集团通过新{bp.ProductId,
                                 bp.ProductName,
                                 bpf.ProductMasterName};

的foreach(在产品VAR组)
{
    VAR键= group.Key;
    //现在可以使用key.ProductName,key.ProductMasterName等。
}
 

至于你设置你的 PRODNAME 变量 - 这正是你想要的是不清楚。第一个产品名称价值?最后?所有这些文件的串联?为什么你需要一个分组呢?

I am writting a group by clause on two tables which are joined and being accessed via Entity Data Model. I am not able to iterate over the anonymous type, can somebody help me out.

 public string GetProductNameByProductId(int productId)
    {
        string prodName=string.Empty;        
        using (VODConnection vodObjectContext = new VODConnection())
        {

            var products = from bp in vodObjectContext.BFProducts
                                             join bpf in vodObjectContext.BFProductMasters on bp.ProductMasterId equals bpf.ProductMasterId
                                             where bp.ProductId == productId
                           group bp by new { ProductId = bp.ProductId, ProductName = bp.ProductName, ProductMasterName=bpf.ProductMasterName} into newInfo
                           select newInfo;

//Want to iterate over products or in fact need to get all the results. How can I do that? Want productmastername property to be set in prodName variable by iterating

            return (prodName);
        }
    }

解决方案

One problem is that you've used a query continuation for no reason. That still shouldn't have prevented you from using the Key property, mind you. Try this as a slightly cleaner approach:

var products = from bp in vodObjectContext.BFProducts
               join bpf in vodObjectContext.BFProductMasters
                 on bp.ProductMasterId equals bpf.ProductMasterId
               where bp.ProductId == productId
               group bp by new { bp.ProductId,
                                 bp.ProductName, 
                                 bpf.ProductMasterName};

foreach (var group in products)
{
    var key = group.Key;
    // Can now use key.ProductName, key.ProductMasterName etc.
}

As for what you set your prodName variable to - it's unclear exactly what you want. The first ProductName value? The last? A concatenation of all of them? Why do you need a grouping at all?

这篇关于写在匿名类型分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆