如何使用不同的对象字段过滤通用字典项?

时间:2014-06-11 19:28:28

标签: c# idictionary

我需要实现一个可以让用户在对象的不同字段上搜索的存储库类。从另一个服务检索数据后,我将结果保存到Dictionary对象,现在我想能够根据不同对象的属性过滤结果。例如:

People {
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

Dictionary<int, People> rawData;

public List<People> GetByFirstName(string firstName) {
    if(string.IsNullOrEmpty(firstName)) {
         return new List<People>();
    }
    return rawData.Where(p => p.FirstName == firstName).ToList(i => i.Value);
}

public List<People> GetByLastName(string lastName) {
    if(string.IsNullOrEmpty(lastName)) {
         return new List<People>();
    }
    return rawData.Where(p => p.LastName == lastName).ToList(i => i.Key, i => i.Value);
}

现在我想通过使用辅助函数来实现某种原型模式:

public List<People> SearchHelper(string searchValue, //need to change so that another can pass condition in here) {
    if(string.IsNullOrEmpty(lastName)) {
         return new List<People>();
    }
    return rawData.Where(// need to put customize condition here).ToList(i => i.Value);
}

我想在这里拥有原型模式的原因是因为我拥有大量的对象属性。任何建议都表示赞赏。

0 个答案:

没有答案