C# 在LINQ中查询两个级别的子集合
我目前有一个linq到实体模型,设置如下 每个样本都有一个测试集合 每个测试都有一组结果 每个结果都有Status属性,用于评估其是否可用或已完成 我如何编写linq查询,该查询将: 获取具有可用结果的样本 仅保留具有可用结果的测试 并且只有每个测试中可用的结果 在解决这个问题和帮助解决这个问题时遇到困难 写下来真的很有帮助 课程:C# 在LINQ中查询两个级别的子集合,c#,.net,linq,asp.net-mvc-3,linq-to-entities,C#,.net,Linq,Asp.net Mvc 3,Linq To Entities,我目前有一个linq到实体模型,设置如下 每个样本都有一个测试集合 每个测试都有一组结果 每个结果都有Status属性,用于评估其是否可用或已完成 我如何编写linq查询,该查询将: 获取具有可用结果的样本 仅保留具有可用结果的测试 并且只有每个测试中可用的结果 在解决这个问题和帮助解决这个问题时遇到困难 写下来真的很有帮助 课程: public class Sample { public Sample() { Tests = new List<Test&g
public class Sample
{
public Sample()
{
Tests = new List<Test>();
}
public int Id { get; set; }
public string IdText { get; set; }
public DateTime SampleDate { get; set; }
public DateTime LoginDate { get; set; }
public string Container { get; set; }
public string Product { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public virtual SamplePoint SamplingPoint { get; set; }
public virtual SampleTemplate SampleTemplate { get; set; }
public virtual Customer ForCustomer { get; set; }
public virtual ICollection<Test> Tests { get; set; }
public class Test
{
public Test()
{
Results = new List<Result>();
}
public string Id { get; set; }
public string Status { get; set; }
public string Analysis { get; set; }
public string ComponentList { get; set; }
public virtual Instrument InstrumentUsed { get; set; }
public virtual ICollection<Result> Results { get; set; }
public virtual Sample ForSample { get; set; }
}
public class Result
{
public string Id { get; set; }
public string TestNumber { get; set; }
public string Status { get; set; }
public string Analysis { get; set; }
public string ComponentName { get; set; }
public string Text { get; set; }
public string Units { get; set; }
public double Value { get; set; }
public int OutOfRange { get; set; }
public DateTime SampledDate { get; set; }
public DateTime SampleLoginDate { get; set; }
public string SamplePoint { get; set; }
public virtual Sample ForSample { get; set; }
public virtual Test ForTest { get; set; }
}
公共类示例
{
公共样本()
{
测试=新列表();
}
公共int Id{get;set;}
公共字符串IdText{get;set;}
public DateTime SampleDate{get;set;}
公共日期时间登录数据{get;set;}
公共字符串容器{get;set;}
公共字符串乘积{get;set;}
公共字符串名称{get;set;}
公共字符串状态{get;set;}
公共虚拟采样点采样点{get;set;}
公共虚拟样本模板样本模板{get;set;}
客户{get;set;}的公共虚拟客户
公共虚拟ICollection测试{get;set;}
公开课考试
{
公开考试()
{
结果=新列表();
}
公共字符串Id{get;set;}
公共字符串状态{get;set;}
公共字符串分析{get;set;}
公共字符串组件列表{get;set;}
使用的公共虚拟仪器{get;set;}
公共虚拟ICollection结果{get;set;}
样本{get;set;}的公共虚拟样本
}
公开课成绩
{
公共字符串Id{get;set;}
公共字符串TestNumber{get;set;}
公共字符串状态{get;set;}
公共字符串分析{get;set;}
公共字符串组件名称{get;set;}
公共字符串文本{get;set;}
公共字符串单位{get;set;}
公共双值{get;set;}
公共int超出范围{get;set;}
公共日期时间采样数据{get;set;}
公共日期时间SampleLoginDate{get;set;}
公共字符串采样点{get;set;}
样本{get;set;}的公共虚拟样本
测试{get;set;}的公共虚拟测试
}
在没有看到实际的类结构的情况下,我希望这能在某种程度上有所帮助:
var testsWithAvailableResults = from test in dbContext.Tests
select new {
Results = (from result in test.Results where result.Status == "Available")
};
如果我了解您的表结构,那么向下查询以获得您感兴趣的结果就相当容易了 我设置了一组简单的类来测试结果
public static class db
{
public static List<Sample> Samples = new List<Sample>();
}
public class Sample
{
public string Name;
public List<Test> Tests = new List<Test>();
}
public class Test
{
public string Name;
public List<Result> Results = new List<Result>();
}
public class Result
{
public string Name;
public string Status;
}
这给了我以下数据:
但这并不能正确地按样本和测试对数据进行分组
一种正确的方法是创建只包含可用结果的新Sample
&Test
对象,如下所示:
var query =
from s in db.Samples
from rt in (
from t in s.Tests
from r in t.Results
where r.Status == "Available"
group r by t into rts
select new Test()
{
Name = rts.Key.Name,
Results = rts.ToList()
})
group rt by s into srts
select new Sample()
{
Name = srts.Key.Name,
Tests = srts.ToList()
};
这将产生以下结果:
但是,可能不可能或不希望创建看起来像实际实体但实际上不是来自数据库的对象的新实例。可能会意外地将其中一个对象保留回数据库并清除正确的记录
因此,我认为最好的替代方法是创建一个嵌套结构,该结构包含未修改的数据库实体,并将可用的测试包含在一个额外的文件中,同时保留嵌套结构
以下是方法:
var query =
from s in db.Samples
from rt in
(from t in s.Tests
from r in t.Results
where r.Status == "Available"
group r by t into rts
select new
{
Test = rts.Key,
AvailableResults = rts.ToArray()
})
group rt by s into srts
select new
{
Sample = srts.Key,
AvailableTests = srts.ToArray()
};
这就产生了:
使用这些结果,您仍然可以访问未更改的示例
和测试
对象,但所有对象都会被可用结果过滤
public static class db
{
public static List<Sample> Samples = new List<Sample>();
}
public class Sample
{
public string Name;
public List<Test> Tests = new List<Test>();
}
public class Test
{
public string Name;
public List<Result> Results = new List<Result>();
}
public class Result
{
public string Name;
public string Status;
}
让我知道这是否有帮助。我需要更高一级的测试是样本的孩子我用类更新了我的帖子,这样你就可以看到我的类结构PS奖金如果我能在流畅的结构中得到它,没有匿名对象我最后想要的是一个带有正确数据的列表基于你的类结构我的中间查询会做什么你想要的很好。你只需要把它连接起来。@AvidProgrammer-你是在问我还是在问OP?@Enigmativity-我在问Chris,他是否能用实体框架来做这件事?很抱歉,这会造成混乱。但答案很有帮助。