3

My understanding is that find only takes the primary key as the parameter. That works great if the value you are looking for is actually the primary key. In my case, I have a class like this:

 public class Chamber
 {
    [Key]
    public int Id {get;set;}

    public string ChamberName { get; set; }
 }

I want to check whether a given ChamberName exists in either my context or the database itself. How can I do that? Do I have to somehow enumerate of the context myself first, then, look it up in the database with a call like db.Chambers.where(a=>a.ChamberName.equals...?

I can see it working well if ChamberName is my primary key, but it is not.

THanks,

2 Answers 2

5

There is a property called Local in the DbSet. You can query that first to find entities loaded to the context.

var entity = db.Chambers.Local.Where(/**/).SingleOrDefault();

if (entity == null)
{
   entity = db.Chambers.Where(/**/).SingleOrDefault();
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Eranga. So there is no way to do it in one shot like find does? that is, search the context, then if not there, the database? thanks for the answer. I did not know about Local.
@PeterKellner Find method internally does something similar. Perhaps you can implement a reusable method.
yes, or extension method. I'm always syntatically challenged when it comes to passing around lamdas.
/**/ what does that do?
@AriesConnolly it's a comment and you can set your search query in a lambda form like so: .Where(e => e.ChamberName.Equals("test"))
2

You can't use the .Find() method - but how about:

public Chamber FindByChamberName(string chamberName) 
{    
   using(MyDbContext ctx = new MyDbContext())
   {
      Chamber result = ctx.Chambers
                          .FirstOrDefault(c => string.Compare(c.ChamberName, chamberName, true));
      return result;
   }
}

You don't have to manually enumerate anything - just retrieve the first occurence of a chamber by that name - or none.

If you just need to know whether a given chamber (specified by its ChamberName) exists or not, you could use the .Any() method in Linq:

using(MyDbContext ctx = new MyDbContext())
{
    return ctx.Chambers.Any(c => string.Compare(c.ChamberName, chamberName, true));
}

3 Comments

marc_s, I don't believe this will work because it does not take into account what is in the context (as find does)
The ignoreCase parameter in Compare seems to be ignored in LINQ to Entities when translated to SQL. I was more interested in the false case (case sensitive search when SQL Server has default collation = case insensitive). I was hoping your code would do the trick (I never used string.Compare before in LTE) but it doesn't work (I just tested it). I'm wondering if there is any way to control case sensitivity from a LTE query. Do you know any? In T-SQL one would use COLLATE SomeCollation in the WHERE clause, or is there any other way?
I naively used the compare thinking it would work. thanks for pointing this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.