LINQ面向父母和孩子的收藏

时间:2018-09-07 14:47:40

标签: linq children selected

以下是我的两个实体

Public Class A {
public int id {get;set}
public string name {get;set;}
public List<B> games {get;set}
}

Public Class B {
public int id {get;set}
public string name {get;set;}
public bool isActive {get;set;}
public int a_id {get;set;}
}

对象A具有以下数据

1, name1 
2, name2
3, name3

对象B具有以下数据

1, cricket, true, 1
2, soccer, false, 1
3, snooket, false, 1

4, cricket, false, 2
5, soccer, true, 2
6, tennis, false, 2
7, poker, false, 2

8, cricket, false, 3
9, soccer, false, 3
10, tennis, true, 3
11, poker, false, 3

我希望我的结果集中仅包含来自对象B的那些行,其中

isActive==true;

我尝试了所有类型的查询,即使用All,Any,但我无法实现目标。

1 个答案:

答案 0 :(得分:1)

您要过滤子列表,在这里您可以使用投影将对象绑定到根据需要过滤的一组新对象。

示例:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using UnityEngine.SceneManagement;

 public static class TransformSaver
 {
     [System.Serializable]
     public class Transform
     {
         public string sceneName;
         public string name;
         public UnityEngine.Transform parent;
         public Vector3 pos;
         public Quaternion rot;
         public Vector3 scale;
     }

     //Save Transform
     public static void SaveTransform(UnityEngine.Transform[] tranformToSave)
     {
         Transform[] trnfrm = new Transform[tranformToSave.Length];
         for (int i = 0; i < trnfrm.Length; i++)
         {
             trnfrm[i] = new Transform();

             trnfrm[i].sceneName = tranformToSave[i].gameObject.scene.name;
             trnfrm[i].name = tranformToSave[i].name;
             trnfrm[i].parent = tranformToSave[i].parent;
             trnfrm[i].pos = tranformToSave[i].localPosition;
             trnfrm[i].rot = tranformToSave[i].localRotation;
             trnfrm[i].scale = tranformToSave[i].localScale;
         }

         string jsonTransform = JsonHelper.ToJson(trnfrm, true);
         //PlayerPrefs.SetString("transform", jsonTransform);
         File.WriteAllText(@"d:\json\json.txt", jsonTransform);
     }

     public static Transform[] LoadTransforms()
     {
         string jsonTransform = File.ReadAllText(@"d:\json\json.txt");
         if (jsonTransform == null)
         {
             return null;
         }

         Transform[] savedTransforms = JsonHelper.FromJson<Transform>(jsonTransform);

         return savedTransforms;
     }
 }