简体   繁体   中英

Linq Query not giving the expected result

After a quite complicated rework of code I stumbled over a small part of code which is not giving me the expected result. The following Linq query should query the data contained in the main object by selecting only those containing a particular string.

The main list contains about 5500 entries. after execution of the Linq query, the secondary object contains still these 5500 entries. Am I blind or already mentally deranged? The CompanyName parameter contains a company which effectively exists.

UPDATE: If the data is taken from the cache the list contains all relevant entries, just the query seems to have no effect.

public List<Account> GetAccountsByValue(string CompanyName)
{
    string CacheKey = "Accounts";
    ObjectCache dataCache = MemoryCache.Default;

    if (dataCache.Contains(CacheKey))
    {
        var ResultCached = (IEnumerable<Account>)dataCache.Get(CacheKey);
        var ResultCached2 = from c in ResultCached
                            where c.Name.Contains(CompanyName)
                            select new
                            {
                              c.Name,
                              c.Street,
                              c.City,
                              c.ID
                            };
        var temp = ResultCached2.ToList();
        return temp;
    }
    else
    {
        IList<CrmAccount> Accounts = CrmServiceAgent.GetAccounts();
        var ResultNoCache = from CrmAccount f in Accounts
                            orderby f.DisplayName
                            select new Account(f);

        // put the data in the cache
        CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
        cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(4.0);
        dataCache.Add(CacheKey, ResultNoCache, cacheItemPolicy);

        return ResultNoCache.ToList();
    }
}

The List of data which is queried looks like this (in WCF Test Client)

在此处输入图片说明

Everything works fine, just only the query part is not having effect which means that again all entries are returned instead of only thos containing the query parameter.

在此处输入图片说明

UPDATE: Actually I get an Null Reference Exception, probably because the Name-Attribute of the first few entries is null....

This is throwing now the error:

var ResultCached3 = ResultCached.Where(c => c.Name.Contains(CompanyName)).ToList();

在此处输入图片说明

I have now fixed the whole thing. The data queried from the database now does not contain any null values anymore. The problem was, that mandatory fields where checked only in the frontend and not on database level. so data which usually is mandatory was imported as null values

so now in the List I have only "valid" data and now the linq query works as it should.

Thank you all for your hints ans suggestions!

kind regards Sandro

 var ResultCached2 = (from c in ResultCached
   where c.Name.Contains(CompanyName)
   select new
   {
    c.Name,
    c.Street,
    c.City,
    c.ID
   }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM