Search code examples
c#linqentity-framework-core

C# EF Core WHERE IN LINQ from List with .Contains return nothing


var ids = new List<string>();

foreach (int rowHandle in this.viewRateManager.GetSelectedRows())
{
    ids.Add(this.viewRateManager.GetRowCellValue(rowHandle, "Id").ToString());
}

var spr = this.contextSQL.ProductSubProductRate
                   .Where(e => e.Id.Contains(ids.ToString()));

DateTime time = this.contextSQL.Database
                   .SqlQuery<DateTime>($"SELECT NOW()")
                   .AsEnumerable()
                   .FirstOrDefault();

foreach (var item in spr)
{
    if (item.Approver != null) { continue; }

    item.Approver = Core.cxCore.ActiveAccount;
    item.Approved = time;
}

this.contextSQL.SaveChanges();

Yesterday I spent the night to finally able to multi-approve the data, and simplify some code. It was a very good sleep. This morning I continued my work, it does not work anymore. Though I moved to another PC via cloud, but should be the same. I am still searching for why it didn't work and why yesterday it works like charm. I want to confirm if I can do it like this?

.Where(e => e.Id.Contains(ids.ToString()));

Because when I removed .Where, it works. So there must be something wrong with the .Contains. I checked ids and it returns exact string identifier correctly.


Solution

  • You wrote the logic with the Contains backwards

    var spr = this.contextSQL.ProductSubProductRate.Where(e => e.Id.Contains(ids.ToString()));
    

    Replace with

    var spr = this.contextSQL.ProductSubProductRate.Where(e => ids.Contains(е.Id));