代码之家  ›  专栏  ›  技术社区  ›  Mike Q

构建C#Linq助手

  •  1
  • Mike Q  · 技术社区  · 14 年前

    我正在尝试使用Linq构建一个实用方法,它将帮助我进行常见的Linq到对象搜索。

    我有一个PropertyTbl对象,它引用了一个地址。 IEnumerable<PropertyTbl> . IEnumerable<Address> 一些搜索条件返回 IEnumerable<地址>

    但是,我不知道如何将这两个插件连接在一起,因为虽然我可以在我的计算机上执行.Select() 为了得到可枚举的地址,我需要 IEnumerable<属性tbl> .

    这是我的助手代码

        public static IEnumerable<Address> BuildAddressWhereClause(IEnumerable<Address> addresses, string value, AddressSearchOptions options)
        {
            string search = value.ToUpper();
    
            if ((options & AddressSearchOptions.Address1To4) == AddressSearchOptions.Address1To4)
            {
                addresses = addresses.Where(o => o.Address1.ToUpper().Contains(search)
                                                 || o.Address2.ToUpper().Contains(search)
                                                 || o.Address3.ToUpper().Contains(search)
                                                 || o.Address4.ToUpper().Contains(search));
            }
    
            if ((options & AddressSearchOptions.City) == AddressSearchOptions.City)
            {
                addresses = addresses.Where(o => o.City.ToUpper().Contains(search));
            }
    
            if ((options & AddressSearchOptions.PostCode) == AddressSearchOptions.PostCode)
            {
                addresses = addresses.Where(o => o.PostCode.ToUpper().Contains(search));
            }
    
            return addresses;
        }
    

    IEnumerable<PropertyTbl> properties = ...;
    
    IEnumerable<PropertyTbl> filteredProperties = <use my address helper somehow>;
    
    // this works but I need the properties as the result not just the addresses
    IEnumerable<Address> filteredAddresses = AddressUtils.FilterAddresses(properties.Select(o => o.Address), "1 High Street", ...);
    

    我可以通过将助手代码直接放入正在加载/筛选属性的代码中来做我想做的事情,但是我不能重用它。

    1 回复  |  直到 14 年前
        1
  •  2
  •   erikkallen    14 年前

    IEnumerable<PropertyTbl> 不是一个 IEnumerable<Address> Address PropertyTable (除非你改变主意 BuildAddressWhereClause 采取行动 IEnumerable<属性tbl>

    像这样的事情应该可以做到:

    public static IEnumerable<T> BuildAddressWhereClause<T>(IEnumerable<T> source, string value, AddressSearchOptions options, Func<T, Address> addressExtractor) { 
        string search = value.ToUpper(); 
    
        if ((options & AddressSearchOptions.Address1To4) == AddressSearchOptions.Address1To4) { 
            source = source.Where(o => addressExtractor(o).Address1.ToUpper().Contains(search) 
                                       || addressExtractor(o).Address2.ToUpper().Contains(search) 
                                       || addressExtractor(o).Address3.ToUpper().Contains(search) 
                                       || addressExtractor(o).Address4.ToUpper().Contains(search)); 
        } 
    
        if ((options & AddressSearchOptions.City) == AddressSearchOptions.City) { 
            source = source.Where(o => addressExtractor(o).City.ToUpper().Contains(search)); 
        } 
    
        if ((options & AddressSearchOptions.PostCode) == AddressSearchOptions.PostCode) { 
            source = source.Where(o => addressExtractor(o).PostCode.ToUpper().Contains(search)); 
        } 
    
        return source; 
    } 
    

    然后你像这样调用它

    IEnumerable<PropertyTbl> properties = ...;      
    IEnumerable<PropertyTbl> filteredProperties = AddressUtils.FilterAddresses(properties, "whatever", AddressSearchOptions.Whatever, p => p.Address);