개체 내에있는 사전을 쿼리하고 전달 된 사전을 기반으로 특정 레코드를 가져와야합니다.
이 있다는 것을 알고 .ContainsKey()
있지만 키 / 값이 서로 일치하는지 확인해야합니다.
나는 그것이 필요하다고 생각 KeyValuePair
하지만 Dictionary <string, string>을 KeyValuePair로 변환하는 방법.
이를 수행하는 가장 좋은 방법은 무엇입니까? 아니면 가능할까요?
암호:
public class MyFiles
{
public int Id { get; set; }
public string FileName { get; set; }
public IDictionary<string, string> Metadata { get; set; } = new Dictionary<string, string>();
}
private void button1_Click(object sender, EventArgs e)
{
var list = new List<MyFiles>();
list.Add(new MyFiles
{
Id = 1,
FileName = "myTestfile.jpg",
Metadata = new Dictionary<string, string>
{
{ "FileSize", "599" },
{ "GroupId", "25" },
{ "TypeId", "Finance" },
}
});
list.Add(new MyFiles
{
Id = 2,
FileName = "myTestfile1.jpg",
Metadata = new Dictionary<string, string>
{
{ "FileSize", "748" },
{ "GroupId", "25" },
{ "TypeId", "HR" },
}
});
list.Add(new MyFiles
{
Id = 3,
FileName = "myTestfile2.jpg",
Metadata = new Dictionary<string, string>
{
{ "FileSize", "78" },
{ "GroupId", "78" },
{ "TypeId", "IT" },
}
});
list.Add(new MyFiles
{
Id = 4,
FileName = "myTestfile2.jpg",
Metadata = new Dictionary<string, string>
{
{ "FileSize", "783" },
{ "GroupId", "78" },
{ "TypeId", "IT" },
}
});
// Instance one ===========================================
var queryDictionary = new Dictionary<string, string>();
queryDictionary.Add("GroupId", "78");
queryDictionary.Add("TypeId", "IT");
// Expected results would be Ids: 3 and 4
// Instance two ===========================================
queryDictionary.Clear();
queryDictionary.Add("GroupId", "78");
queryDictionary.Add("FileSize", "783");
// Expected results would be Ids: 4
}
모든 키 / 값 쌍을 반복 queryDictionary
하고 목록의 각 항목에 대해 일치시켜야합니다.
var results = list.Where(
mf => queryDictionary.All(
kvp => mf.Metadata.TryGetValue(kvp.Key, out var metdata) && metadata == kvp.Value
)
);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다