简体   繁体   中英

C# Linq - How would I apply filter in which way it could not compare itself

I am working on a project where I would like to update Name column of Areas table. While updating I wanted to validate if there is same Areas.Name available in database but while searching/comparing it should not compare with itself. I come up with below query which searches all values of database column Areas.Name but could not skip comparing itself.

    public bool IsDuplicateArea(string areaName)
    {
        return db.Areas.Where(a => a.Name == areaName).Any();
    }

Where areaName contains the name of area which I would like to update in database.

Any one have an idea how would I apply filter in which way it could not compare itself?

pass the ID of the column and check it like follows

public bool IsDuplicateArea(string areaName, int Id)
{
    return db.Areas.Where(a => a.Name == areaName && a.Id != Id).Any();
}

您也只能使用.Any()

return db.Areas.Any(a => a.Name == areaName && a.Id != Id);

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