有效的方式来查询嵌套数据 [英] Efficient Way To Query Nested Data

查看:95
本文介绍了有效的方式来查询嵌套数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从表中选择一个数字'主人'的行,也从另一个表返回每个结果的一些细节行。什么是实现这一目标没有多个查询(一个用于主行和每个结果,以获得详细信息行一个)的好方法。



例如,与数据库结构象下面这样:

  MasterTable:
- MasterId BIGINT
- 名称NVARCHAR(100)

DetailTable:
- DetailId BIGINT
- MasterId BIGINT
- 资金量

我将如何最有效地填充数据下面?

 <$对象C $ C>的IList< MasterDetail>数据; 

公共类硕士
{
私人只读表<详情> _details =新的List<详情>();

众长MasterId
{
获得;组;
}

公共字符串名称
{
获得;组;
}

公众的IList<详情>详情
{
得到
{
返回_details;
}
}
}

公共类详细
{
众长DetailId
{
搞定;组;
}

公共小数金额
{
获得;组;
}
}


解决方案

通常,我会去为两格的做法 - 但是,你可能也想看看FOR XML - 这是相当容易的(在SQL Server 2005及以上)塑造的父/子数据为XML,并从那里加载它

  SELECT父。*,
(SELECT * FROM孩子
其中child.parentid =父。 ID FOR XML PATH('孩子'),TYPE)
从父
FOR XML PATH(父)

此外 - LINQ到SQL支持这种类型的模型,但你需要告诉它你想要的时间提前的数据。通过 DataLoadOptions.LoadWith

  //从MSDN 
Northwnd DB =样品新Northwnd(@C:\\\
orthwnd.mdf);
DataLoadOptions DLO =新DataLoadOptions();
dlo.LoadWith<客户>(C => c.Orders);
db.LoadOptions = DLO;

变种londonCustomers =
从卡斯特在db.Customers
其中cust.City ==伦敦
选择卡斯特;

的foreach(在londonCustomers VAR custObj)
{
Console.WriteLine(custObj.CustomerID);
}

如果你不使用 LoadWith ,您将获得N + 1查询 - 一个主,每主排一个孩子列表


I have need to select a number of 'master' rows from a table, also returning for each result a number of detail rows from another table. What is a good way of achieving this without multiple queries (one for the master rows and one per result to get the detail rows).

For example, with a database structure like below:

MasterTable:
    - MasterId BIGINT
    - Name NVARCHAR(100)

DetailTable:
    - DetailId BIGINT
    - MasterId BIGINT
    - Amount MONEY

How would I most efficiently populate the data object below?

IList<MasterDetail> data;

public class Master
{
    private readonly List<Detail> _details = new List<Detail>();

    public long MasterId
    {
        get; set;
    }

    public string Name
    {
        get; set;
    }

    public IList<Detail> Details
    {
        get
        {
            return _details;
        }
    }
}

public class Detail
{
    public long DetailId
    {
        get; set;
    }

    public decimal Amount
    {
        get; set;
    }
}

解决方案

Normally, I'd go for the two grids approach - however, you might also want to look at FOR XML - it is fairly easy (in SQL Server 2005 and above) to shape the parent/child data as xml, and load it from there.

SELECT parent.*,
       (SELECT * FROM child
       WHERE child.parentid = parent.id FOR XML PATH('child'), TYPE)
FROM parent
FOR XML PATH('parent')

Also - LINQ-to-SQL supports this type of model, but you need to tell it which data you want ahead of time. Via DataLoadOptions.LoadWith:

// sample from MSDN
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Customer>(c => c.Orders);
db.LoadOptions = dlo;

var londonCustomers =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach (var custObj in londonCustomers)
{
    Console.WriteLine(custObj.CustomerID);
}

If you don't use LoadWith, you will get n+1 queries - one master, and one child list per master row.

这篇关于有效的方式来查询嵌套数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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