c# - LINQ to SQL - 具有 List<T> 属性的自定义对象

标签 c# linq-to-sql

我正在尝试做的是:

我有一个自定义对象 OTest 和 OSubTest:

public class OTest
{
   public int TestId {get; set;}
   public List<OSubTest> SubTests {get; set;}
}

public class OSubTest
{
   public int SubTestId {get; set;}
}

现在,我将运行 LINQ to SQL 查询,如下所示:

var query = from t in Tests 
select new OTest
{
   TestId = t.TestId
};

问题是,如果没有 LINQ 从数据的主查询运行 100 个子查询,我如何才能将“子测试”作为查询的一部分并作为我的主要 OTest 对象中的列表属性。

如果我这样做,那么它将对返回的每条记录执行查询,这不是很实用:

var query = from t in Tests 
select new OTest
{
   TestId = t.TestId,
   SubTests = (from st in SubTests where st.TestId = t.TestId select new OSubTest{SubTestId = st.SubTestId}).ToList()
};

关于如何让我的 List 属性填充单个查询有什么想法吗?也许用左连接?

如有任何帮助,我们将不胜感激。

如果您需要任何说明,请问我。

编辑

好的,看看这个,当我有 1 个子列表时它确实有效,但是一旦我添加第二个,第二个就会查询数据库 100 次。这是我的确切查询:

public List<OFile> GetFiles(int fileGroupId)
        {
            var query = (from f in CoreContext.Files
                         join fgf in CoreContext.FileGroupFiles on f.FileId equals fgf.FileId
                         where fgf.FileGroupId == fileGroupId
                         select new OFile
                                    {
                                        ActiveFile = f.ActiveFile,
                                        FileExtension = f.FileType,
                                        FileGuid = f.FileIdentifier,
                                        FileName = f.FileName,
                                        FilePath = f.FilePath,
                                        FileSize = f.FileSize,
                                        FileTitle = f.FileTitle,
                                        RevisionId = f.RevisionId,
                                        ParentFileId = f.ParentFileId,
                                        FileId = f.FileId,
                                        Attributes = f.FileAttributeFiles.Where(id => id.FileId == f.FileId).Select(
                                            id => new OFileAttribute
                                                      {
                                                          FileAttributeId = id.FileAttributeId,
                                                          Value = id.Value
                                                      }).ToList(),
                                        Tags = f.FileTags.Where(id => id.FileId == f.FileId).Select(
                                            id => new OFileTag
                                                      {
                                                          FileTagId = id.FileTagId,
                                                          TagName = id.TagName,
                                                      }).ToList(),


                                    });



            return query.ToList();
        }

属性将查询数据库 100 次并且不会显示为 JOIN。

FileAttributeFiles 表具有到 Attributes 表的外键。

谢谢

最佳答案

If I did it like this then it will execute a query for each record that is returned, which is not very practical

嗯,真的吗?你试过了吗?我不确定 TestsSubTests 应该是什么,但一般来说,LINQ to SQL 执行这种形式的查询:

using (var c = new MyDataContext())
{
    var q = from t in c.Tests
            select new 
                   {
                       TestId = t.TestId,
                       SubTests = from st in t.SubTests 
                                  where st.TestId = t.TestId 
                                  select new 
                                  {
                                      SubTestId = st.SubTestId
                                  }
                    };
}

...在一次查询中。投影到 POCO 而不是匿名类型也可以。我不知道使用 ToList() 是否会改变任何东西,因为我个人不使用它;没有它它“就可以工作”。

关于c# - LINQ to SQL - 具有 List<T> 属性的自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2420744/

相关文章:

c# - 从问题中生成不同路径的矩阵?,排列?,组合?

c# - 当使用 C# 使用 SOAP Web 服务时,获取为 null,提供程序为 Ladon/Python

c# - 高效判断类型

c# - 使用 LinQ 创建表时无法确定...的 SQL 类型

linq-to-sql - 如何在 linq to sql 中执行全选(*)

c# - 是否有一个很好的工具可以从 XAML 中查找对属性的引用?

c# - 如何在 NotifyIcon 中使用 MouseWheel

c# - 使用 sum 和 group by 将 Sql Query 转换为 linq

c# - 使用 Linq to Sql 自动序列化

c# - 在存储过程中使用 LIKE 命令