Почему этот LINQ к SQL-запросу получает NotSupportedException?

Я думаю, что тире лучше с пользовательской точки зрения, и он не вмешается в SEO.

Не уверенный, где или почему запущенное соглашение подчеркивания.

Немного более хорошо осведомленный дебаты

7
задан Edward Tanguay 8 December 2009 в 14:02
поделиться

2 ответа

Ed, I've run into a similiar situation. The code is below. The important line of code is where I set the memberList variable. See if this fits your situation. Sorry if the formatting didn't come out to well.

Randy

// Get all the members that have an ActiveDirectorySecurityId matching one in the list.
IEnumerable<Member> members = database.Members
   .Where(member => activeDirectoryIds.Contains(member.ActiveDirectorySecurityId))
   .Select(member => member);

// This is necessary to avoid getting a "Queries with local collections are not supported"
//error in the next query.    
memberList = members.ToList<Member>();

// Now get all the roles associated with the members retrieved in the first step.
IEnumerable<Role> roles = from i in database.MemberRoles
   where memberList.Contains(i.Member)
   select i.Role;
1
ответ дан 7 December 2019 в 18:44
поделиться

Поскольку вы не можете объединить локальную последовательность с таблицей linq, единственный способ перевести приведенный выше запрос в SQL - создать предложение WHERE с таким количеством условий LIKE, сколько элементов в списке searchTerms (объединены с операторами И). По-видимому, linq не делает этого автоматически и вместо этого выдает исключение. But it can be done manually by iterating through the sequence:

public override List<Item> SearchListWithSearchPhrase(string searchPhrase)
{
    List<string> searchTerms = StringHelpers.GetSearchTerms(searchPhrase);

    using (var db = Datasource.GetContext())
    {
        IQueryable<Task> taskQuery = db.Tasks.AsQueryable();
        foreach(var term in searchTerms)
        {
              taskQuery = taskQuery.Where(t=>t.Title.ToUpper().Contains(term.ToUpper()) && t.Description.ToUpper().Contains(term.ToUpper()))            
        }
        return taskQuery.ToList();
    }
}

Mind that the query is still executed by DBMS as a SQL statement. The only drawback is that searchTerms list shouldn't be to long - otherwise the produced SQL statement won'tbe efficient.

1
ответ дан 7 December 2019 в 18:44
поделиться
Другие вопросы по тегам:

Похожие вопросы: