在匿名类型上编写 Group By

发布于 2024-11-02 05:27:49 字数 982 浏览 1 评论 0原文

我正在两个表上编写一个 group by 子句,这两个表已连接并通过实体数据模型进行访问。我无法迭代匿名类型,有人可以帮助我吗?

 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);
        }
    }

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);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-11-09 05:27:49

一个问题是您无缘无故地使用了查询延续。请注意,这仍然不应该阻止您使用 Key 属性。尝试使用一种稍微简洁的方法:

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.
}

至于将 prodName 变量设置为什么 - 目前还不清楚您到底想要什么。第一个 ProductName 值?最后一个?所有这些的串联?为什么需要分组?

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?

傲娇萝莉攻 2024-11-09 05:27:49
foreach(var prod in products)
{
  prodName += prod.Key.ProductMasterName;
}
foreach(var prod in products)
{
  prodName += prod.Key.ProductMasterName;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文