1

好的,我正用这个把头撞到墙上;-)

给定我的数据库中名为 Address、Customer 和 CustomerType 的表,我想显示有关客户的组合摘要信息,因此我创建了一个查询来连接这两个表并检索指定的结果。

 var customers = (from c in tblCustomer.All()
                        join address in tblAddress.All() on c.Address equals address.AddressId
                        join type in tblCustomerType.All() on c.CustomerType equals type.CustomerTypeId
                        select new CustomerSummaryView
                                   {
                                       CustomerName = c.CustomerName,
                                       CustomerType = type.Description,
                                       Postcode = address.Postcode
                                   });

  return View(customers);

CustomerSummaryView 是一个简单的 POCO

public class CustomerSummaryView
{
    public string Postcode { get; set; }
    public string CustomerType { get; set; }
    public string CustomerName { get; set; }
}

现在由于某种原因,这不起作用,我得到一个 CustomerSummaryView 结果的 IEnumerable 列表,每条记录都有一个客户姓名和一个邮政编码,但客户类型字段始终为空。

我已经多次使用不同的数据库表和投影类重新创建了这个问题。

有人有什么想法吗?

4

4 回答 4

2

我无法重现这个问题 - 这是我刚刚尝试过的一个测试:

[Fact]
public void Joined_Projection_Should_Return_All_Values() {
    var qry = (from c in _db.Customers
                     join order in _db.Orders on c.CustomerID equals order.CustomerID
                     join details in _db.OrderDetails on order.OrderID equals details.OrderID
                     join products in _db.Products on details.ProductID equals products.ProductID
                     select new CustomerSummaryView
                     {
                         CustomerID = c.CustomerID,
                         OrderID = order.OrderID,
                         ProductName = products.ProductName
                     });

    Assert.True(qry.Count() > 0);

    foreach (var view in qry) {
        Assert.False(String.IsNullOrEmpty(view.ProductName));
        Assert.True(view.OrderID > 0);
        Assert.False(String.IsNullOrEmpty(view.CustomerID));
    }

}

这完美地通过了。我想知道您是否在其中使用了保留字?

于 2009-07-15T19:54:17.283 回答
0

是的,Rob 的示例有效的原因是因为他的投影的属性名称完全匹配,而 John 的原始示例在 CustomerType 和 type.Description 之间存在差异。

这不应该是一个问题,但它是 - 投影映射器正在寻找同名的属性,如果它没有找到匹配项,则不会映射一个值。因此,如果名称不完全匹配,您的投影对象的属性将是其类型的默认值。

好消息是,我今天获得了最新的源代码并构建了一个新的 Subsonic.Core.dll,并且该行为现在已修复。

所以上面约翰的代码应该可以按预期工作。

于 2010-01-27T02:00:26.477 回答
0

I just downloaded the latest build from 3/21/2010, which is about 2 months after the last poster on this thread, and the problem still exists in the packaged binary. Bummer.

Here what I have to do:

        var data =
            (from m in Metric.All()
             where m.ParentMetricId == parentId
             select new
                    {
                         m.MetricName,
                         m.MetricId,
                    })
                    .ToList();

        var treeData =
            from d in data
            select new TreeViewItem
                    {
                        Text = d.MetricName,
                        Value = d.MetricId.ToString(),
                        LoadOnDemand = true,
                        Enabled = true,
                    };

        return new JsonResult { Data = treeData };

If I try to do the projection directly from the Subsonic query, the Text property ends up with the ID, and the Value property ends up with the Name. Very strange.

于 2010-09-30T15:28:47.203 回答
0

这个帖子似乎是指类似的问题......

http://groups.google.com/group/subsonicproject/browse_thread/thread/2b569539b7f67a34?hl=en&pli=1

于 2009-07-29T10:46:09.103 回答