应用位置过滤器的最佳做法

时间:2013-11-28 18:45:33

标签: c# asp.net-mvc linq

我希望有一个更清晰的代码来对初始值集进行过滤。

这段代码很有效,但非常难看,问题是我找不到为splitKeywords.Contains(ks.Name)创建linq表达式的方法keywordsAddressIds.Where(splitKeywords.Contains(ks.Name))

    var addressPTQuery = _addressPTRepository.Query;
    var categoryQuery = _categoryRepository.Query; 
    var keywordSearchQuery = _keywordSearchRepository.Query;

    // Initial value of keywordsAddressIds //
    var keywordsAddressIds = from ks in keywordSearchQuery where splitKeywords.Contains(ks.Name) select ks.Keyword.Addresses.Select(k => k.Id);

    var searchResults = new CategoryGetAllBySearchListDto();

    if (isUserCenter)
    {
        // Filter to keywordsAddressIds  //

        DbGeography centerPoint = Map.CreatePoint(userLng, userLat);

        // radius is in kms so we have to x 100 to get meters
        ks.Keyword.Addresses.Select(ad => ad.Id) == addressPTQuery.Where(p => p.Location.Distance(centerPoint) < radius * 1000).Select(adPT => adPT.Id));

        keywordsAddressIds = from ks in keywordSearchQuery
                             where splitKeywords.Contains(ks.Name)
                             select ks.Keyword.Addresses.Where(p => p.Location.Distance(centerPoint) < radius * 1000).Select(a => a.Id);
    }
    else if (!string.IsNullOrEmpty(location))
    {
        // Filter to keywordsAddressIds  //

        string locationStandard = CleanLocation(location);

        addressPTQuery = addressPTQuery.Where(p => p.PTCouncil.NameStandard == locationStandard);

        keywordsAddressIds = from ks in keywordSearchQuery
                             where splitKeywords.Contains(ks.Name)
                             select ks.Keyword.Addresses.Select(a => a.Id).Intersect(addressPTQuery.Select(ad => ad.Id));
    }

1 个答案:

答案 0 :(得分:0)

那么为什么你没有得到所有的地址id然后选择你想要的东西呢

var addressPTQuery = _addressPTRepository.Query;
var categoryQuery = _categoryRepository.Query; 
var keywordSearchQuery = _keywordSearchRepository.Query;

// Initial value of keywordsAddresses //
var keywordsAddresses = from ks in keywordSearchQuery 
                        where splitKeywords.Contains(ks.Name) 
                        select ks.Keyword.Addresses;

var searchResults = new CategoryGetAllBySearchListDto();

if (isUserCenter)
{
    // Filter to keywordsAddressIds  //

    DbGeography centerPoint = Map.CreatePoint(userLng, userLat);

    // radius is in kms so we have to x 100 to get meters
    ks.Keyword.Addresses.Select(ad => ad.Id) == addressPTQuery.Where(p => p.Location.Distance(centerPoint) < radius * 1000).Select(adPT => adPT.Id));

    keywordsAddressIds = from p in keywordsAddresses 
                         where p.Location.Distance(centerPoint) < radius * 1000
                         select p.Id;
}
else if (!string.IsNullOrEmpty(location))
{
    // Filter to keywordsAddressIds  //

    string locationStandard = CleanLocation(location);

    addressPTQuery = addressPTQuery.Where(p => p.PTCouncil.NameStandard == locationStandard);

    keywordsAddressIds = from a in keywordsAddresses
                         join b in addressPTQuery on a.Id equals b.Id
                         select a.Id;
}