Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示集合子集合的特定元素_C#_Linq - Fatal编程技术网

C# 显示集合子集合的特定元素

C# 显示集合子集合的特定元素,c#,linq,C#,Linq,我有一个列表集合,其中包含一个列表子集合作为属性,我希望根据某些属性的值筛选出该子集合中的项 为了简化,我将调用主集合和子集合子项。它们是不同的类型。事物可以有1到多个子事物。子项有两个我想筛选的属性,PROP1应该等于1,它可以等于1,2,3,PROP2不应该为NULL,它可以包含字符串 因此,当我使用下面这样的查询时,它似乎给了我想要的东西,尽管我不确定所有这些都在做我期望的事情: search = from c in search where c.SUBTHING.All(s=>s.

我有一个列表集合,其中包含一个列表子集合作为属性,我希望根据某些属性的值筛选出该子集合中的项

为了简化,我将调用主集合和子集合子项。它们是不同的类型。事物可以有1到多个子事物。子项有两个我想筛选的属性,PROP1应该等于1,它可以等于1,2,3,PROP2不应该为NULL,它可以包含字符串

因此,当我使用下面这样的查询时,它似乎给了我想要的东西,尽管我不确定所有这些都在做我期望的事情:

search = from c in search
where c.SUBTHING.All(s=>s.PROP1==1)
select c;
当我添加其他属性时,我会产生怀疑:

search = from c in search
where c.SUBTHING.All(s=>s.PROP1==1 && s.PROP2 != NULL)
select c;
我得到的结果是PROP2为Null

当我切换到Any时,我会丢失所有对子内容的过滤,它会显示子内容,其中PROP1=1,2,3,其中PROP2为NULL且不为NULL

我想得到的是一个集合,它列出了所有事物ID,然后列出了所有子事物的名称,有点像这样:

THING.ID
     SUBTHING.Name
     SUBTHING.Name

THING.ID
     SUBTHING.Name
     SUBTHING.Name
search = 
    from c in search 
    where c.SUBTHING.All(s=>s.PROP1==1 && s.PROP2 != NULL) 
    select new {
         ThingId = c.ThingID,
         Something = c.SomeThing.Select(x=>x.Name)
     }; 
var filtered = from c in search
               select new Thing(c, c.Subthings.Where(x => x.PROP1 == 1 && x.PROP2 != null))

由于THING和SUBTHING是两种不同的类型,因此在使用LINQ过滤内容时是否也可以过滤子内容?

尝试以下方法:

THING.ID
     SUBTHING.Name
     SUBTHING.Name

THING.ID
     SUBTHING.Name
     SUBTHING.Name
search = 
    from c in search 
    where c.SUBTHING.All(s=>s.PROP1==1 && s.PROP2 != NULL) 
    select new {
         ThingId = c.ThingID,
         Something = c.SomeThing.Select(x=>x.Name)
     }; 
var filtered = from c in search
               select new Thing(c, c.Subthings.Where(x => x.PROP1 == 1 && x.PROP2 != null))
要对子项应用筛选器,请尝试:

from product in products
where product.productid == 1
from image in product.productimages
where image.ismainimage
select image.imagename
From:

一种方法是使用可枚举。其中和匿名类型:

var result = from thing in search
         from subthing in thing.subthings
         where subthing.prop1 == 1 && subthing.prop2 != null
         select new {ID = thing.ID, Name = subthing.Name};

foreach(var x in result)
{
    Console.WriteLine("ID={0} Name{1}", x.ID, x.Name);
}

在查询父实体时,需要投影,但在结果集中,您只希望有其子实体的子集

您可以通过以下方式进行操作:

class Thing
{
    Thing(Thing original, IEnumerable<Subthing> subthings)
    {
        // Initialize based on original and set the collection
        //
        ...
    }
}

我不确定这些答案是否真的能满足你的需求,尽管它们很接近。根据我的理解,您需要一个列表,其中至少有一个子项具有您在本例中感兴趣的值,Prop1==1和Prop2!=无效的这里有一些选择,只是取决于你是从一个事物还是一个微妙的角度来工作

选项1:事物方法

你看到的任何东西都与你的身体状况有关。因此:

var result = from thing in search
             where thing.Subthings.Any(tr => tr.Prop1 == 1 && tr.Prop2 != null)
             select new { ID = thing.ID, Names = thing.Subthings.Where(tr => tr.Prop1 == 1 && tr.Prop2 != null) };
备选方案2:子划分方法

您正在查看所有子任务,并找到满足条件的子任务,按该点的ID进行分组

var result = from thing in search
             from sub in thing.Subthings
             where sub.Prop1 == 1 && sub.Prop2 != null
             group sub by thing.id into sg
             select new { ID = sg.Key, Names = sg.Select(tr => tr.Name) };
我更喜欢这种方法,但仍有改进的余地。我喜欢这样做的原因是,您首先找到子项,然后它才会拉取与其关联的对象,而不是首先必须找到是否有子项符合条件,然后选择它

备选办法3:混合办法

这两者都有一点。我们将以任何一种方式从子内容中进行选择,因此最好只执行选择。然后,如果任何投影的子集合有任何元素,那么我们返回带有名称的对象

在我看来,这是最干净的选择。如果集合中有任何项,则不带任何参数的集合上的Any扩展方法将返回true。非常适合我们的情况


希望这能有所帮助,让我们知道您的想法。

我想得到的是一个列出所有子项ID的集合。。。是一个打字错误,应该是我想得到的是一个集合,列出所有的东西ID@ewomack,你是否试图在搜索中修改c,使其具有与所有内容完全相同的值,但子内容被过滤?如果我理解正确,你希望整个层次结构中的所有子内容都具有PROP1=1和PROP2=Null及其对应的THING.id。。。。是吗?@Tim,是的,你是对的,那是个打字错误。固定的谢谢大家!@M Afifi&DarkSquirrel,是的,你们听起来都是正确的-我试图从事物->子事物的层次结构中过滤子集合子事物。