简体   繁体   English

如何从array1中选择所有包含array2的ID?

[英]How to select all from array1 which contains the id from array2?

I have a linq statement which includes a Contain() method. 我有一个linq语句,其中包含一个Contain()方法。 I am using this so that I can select all from an array where name is not null but only select the objects from the array1 that contains the same name in object array2. 我正在使用它,以便可以从名称不为null的数组中选择所有对象,而仅从array1中包含对象array2中包含相同名称的对象中选择对象。

I have managed to return the result but its displaying true or false where as I need the object values. 我设法返回了结果,但是在需要对象值的地方显示了true或false。

The code 编码

var response = JsonConvert.DeserializeObject<FamilyNames>(result);
List<object> data = new List<object>();

ClassName className = new ClassName();

object [] getNames = className.GetType()
     .GetProperties()
     .Select(p =>
       {
         object value = p.Name;
         return value == null ? null : value.ToString();
       })
         .ToArray();

foreach (var obj in response.items.Where(n => n.name != null).DistinctBy(x => x.name).Select(a => getNames.Contains(a.initialName)))
{
     data.Add(obj);
}
client.Dispose();
return Json(data, JsonRequestBehavior.AllowGet);
}

The result is : 结果是:

["True","False","True"]

If I don't use the select statement then I get my objects: 如果不使用select语句,则会得到对象:

[
  {
    "initalName": "BD",
    "firstName": "Bob",
    "LastName": "Dilan"
  },
  {
    "initalName": "HT",
    "firstName": "Harry", // the initialName doesn't exist in list so need to remove this object
    "LastName": "Thomas"
  },
  {
    "initalName": "LJ",
    "firstName": "Lindsey",
    "LastName": "Jones"
  }
]

The initalName is not present in getNames array so needs to be removed. initalName在getNames数组中不存在,因此需要删除。 Any advice would be much appreciated, especially on the approach. 任何建议将不胜感激,尤其是在方法上。 The desired result would be: 理想的结果将是:

[
      {
        "initalName": "BD",
        "firstName": "Bob",
        "LastName": "Dilan"
      },
      {
        "initalName": "LJ",
        "firstName": "Lindsey",
        "LastName": "Jones"
]

The problem is, that in this LINQ expression, at the end you are selecting a Bool as an output. 问题在于,在此LINQ表达式中,最后您选择的是Bool作为输出。 (.Contains() returns a bool). (.Contains()返回布尔值)。 From this reason, your expression will retunr a list of Bool. 因此,您的表达式将重新调整Bool列表。

response.items
.Where(n => n.name != null)
.DistinctBy(x => x.name)
.Select(a => getNames.Contains(a.initialName))

To acheave what you want, simply replace the .Select() with a .Where(), what will do the intended filtering and keep the original objects as they are, (will not do any projection) and you will get the expected outcome: 要满足您的需求,只需将.Select()替换为.Where(),将执行预期的过滤并保持原始对象不变(不会进行任何投影),您将获得预期的结果:

response.items
.Where(n => n.name != null)
.DistinctBy(x => x.name)
.Where(a => getNames.Contains(a.initialName))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 array1 打印 item[0] 并完成 array2 的结果(与 item[1] 重复) - How can I print item[0] from array1 and complete the result from array2 (repeated with item[1]) 我需要检查 Array1 中的 CHAR 是否包含我的 Array2 中的一些 CHAR,但我不能将 Contains 用于 CHAR... 如何解决? - I need to check if CHAR from Array1 contains some CHAR from my Array2 but I cant use Contains for CHAR... How to solve it? C#-Array2包含Array1忽略顺序 - C# - Array2 contains Array1 ignore order 如何从包含字符串数组的 JSON 中选择值? - How to select value from a JSON which contains a string array? 如何从数组中选择包含一个相等值和一个更大值并用逗号分隔的行? - How select line from array which contains one equal value and one greater, separated by comma? C# Linq to entity - 如何仅选择具有数组中所有引用的记录 - C# Linq to entities - How to select only records which have all references from array 从包含另一个数组的值的数组中返回所有值 - Return all values from an array that contains the value from another array 从中选择ID(我的程序的数组) - Select from where ID in (an array of my program) 如何 select 包含数组中字符串的所有名称? (EF 和 Linq) - How to select all Names that contain a string from an array? (EF and Linq) 如何从字符串中获取包含每六个(或第n个)单词的数组? - How to obtain an array which contains every sixth (or nth) word from a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM