Linq 按字典值搜索

Linq Search by Dictionary value

提问人:Asana 提问时间:11/13/2023 最后编辑:EnigmativityAsana 更新时间:11/13/2023 访问量:82

问:

我正在尝试通过字典键和值搜索包含List<Dictionary<string,string>>的联系人(对象)

以下是我的 Json 联系人阵列

[
  {
    "first_name": "David",
    "last_name": "Smith",
    "email": "[email protected]",
    "phone": "1234567890",
    "website": "google.com",
    "relations": [
      {
        "listid": "65512fe1e759b98f40b48829"
      },
      {
        "listid": "34212fe1e759b98f40b48829"
      }
    ]
  },
  {
    "first_name": "Chris",
    "last_name": "Oven",
    "email": "[email protected]",
    "phone": "1234567890",
    "website": "google.com",
    "relations": [
      {
        "listid": "65512fe1e759b98f40b48829"
      },
      {
        "listid": "34212fe1e759b98f40b48829"
      }
    ]
  }
]

我正在尝试查找包含listid =“65512fe1e759b98f40b48829”的所有联系人。

使用 Linq 执行此操作的最佳方法是什么?

尝试了如下方法:

var searchPair = new KeyValuePair<string, string>("listid", "65512fe1e759b98f40b48829");

contacts.Where(p=>p.relations.Where(dictionary => dictionary[searchPair.Key].ToString().Contains(searchPair.Value)).ToList();

但是在某个地方它给出了不正确的搜索方式

C# .NET LINQ-to-Entities

评论

1赞 T N 11/13/2023
也许像.contacts.Where(p => p.relation.Any(r => r.Key == searchPair.Key && r.Value == searchPair.Value))

答:

1赞 Enigmativity 11/13/2023 #1

在我看来,这是您的 JSON 的更好表示:

public class Customer
{
    [JsonProperty("first_name")]
    public string FirstName { get; set; }

    [JsonProperty("last_name")]
    public string LastName { get; set; }

    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("phone")]
    public string Phone { get; set; }

    [JsonProperty("website")]
    public string Website { get; set; }

    [JsonProperty("relations")]
    public List<Relation> Relations { get; set; }
}

public class Relation
{
    [JsonProperty("listid")]
    public string ListId { get; set; }
}

现在,您可以读取数据:

List<Customer> customers =
    Newtonsoft
        .Json
        .JsonConvert
        .DeserializeObject<List<Customer>>(
            File.ReadAllText(@"customers.json"));

这给出了:

Deserialized JSON

现在查询它很简单:

IEnumerable<Customer> query =
    from c in customers
    where c.Relations.Any(r => r.ListId == "65512fe1e759b98f40b48829")
    select c;

如果您使用建议的字典版本,则查询如下:relations

IEnumerable<Customer> query =
    from c in customers
    where c.Relations.Any(r => r.TryGetValue("listid", out string value)
        && value == "65512fe1e759b98f40b48829")
    select c;

评论

0赞 Asana 11/13/2023
public class Customer { [JsonProperty(“first_name”)] public string FirstName { get; set; }[JsonProperty(“last_name”)] public string LastName { get; set;[JsonProperty(“email”)] public string Email { get; set; }[JsonProperty(“phone”)] public string Phone { get; set;[JsonProperty(“website”)] public string Website { get; set;[JsonProperty(“relations”)] public List<Dictionary<string, string>>?关系 { get; set;
0赞 Asana 11/13/2023
编辑了您的客户类以重新选择我上面的内容
0赞 Enigmativity 11/13/2023
@Asana - 为什么要列出词典?您的数据似乎不需要它。
0赞 Enigmativity 11/19/2023
@Asana - 请不要这样编辑别人的答案。