This project is read-only.

IRepository<T>.Get(Expression<Func<T, bool>> predicate))

Topics: General, Troubleshooting
Nov 4, 2011 at 11:55 AM

Hi everyone,

I have a very strange behavior with this method and I'm getting a headache since it's not possible to debug Linq to Sql.

Here's my problem:

var link = _linkRepository.Get(x =>
                x.AttributeInstanceID == attributeInstance.Id);

if (link == null)
     link = _linkRepository.Get(attributeInstance.Id);
     Debug.WriteLine(link.AttributeInstanceID); // => = attributeInstance.Id!!!!

I always get null with the first instruction whereas I find something with the second Get and by chance link.Id = link.AttributeInstanceID. It can't find the link for which AttributeInstanceID equals to attributeInstance.Id BUT it can find a link for which Id equals to attributeInstance.Id and by chance its AttributeInstanceID equals to it too. Why does it not find it at first place?!!!


Strangely the following line works though:

var link = _linkRepository.Fetch(x => true)
                .Where(x => x.AttributeInstanceID == attributeInstance.Id).First();

I tried this also but it returns all the objects so there is an exception (as if Test always returns true where as there is only one object which matches this clause!):


var link = _linkRepository.Get(x => Test(x, attributeInstance.Id));

public bool Test(AttributeInstanceHasAttributeValues rec, AttributeInstanceRecord attributeInstance)
  return rec.AttributeInstanceID == attributeInstance.Id;


Here are the classes and the declarations of the variables:


private readonly IRepository<AttributeInstanceHasAttributeValuesRecord>

public class AttributeInstanceHasAttributeValuesRecord
        public virtual int Id { get; set; }
        public virtual int AttributeInstanceID { get; set; }
        public virtual int AttributeValueID { get; set; }

AttributeInstanceRecord attributeInstance;

public class AttributeInstanceRecord
        public virtual int Id { get; set; }
        public virtual int AttributeID { get; set; }

Nov 4, 2011 at 1:00 PM

Although I'm a bit confused about the problem, the reason that

var link = _linkRepository.Get(x =>
                x.AttributeInstanceID == attributeInstance.Id);
returns null but
var link = _linkRepository.Fetch(x => true)
                .Where(x => x.AttributeInstanceID == attributeInstance.Id).First();
returns the data could be that there are more than one items in the table that have the same "Id". Is Id a primary key?
Nov 4, 2011 at 1:42 PM

Yes it is


SchemaBuilder.CreateTable("AttributeInstanceHasAttributeValuesRecord", table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
                .Column<int>("AttributeInstanceID", column => column.WithLength(10))
                .Column<int>("AttributeValueID", column => column.WithLength(10))

Nov 4, 2011 at 5:15 PM

And it's guaranteed that there are no multiple AttributeInstanceHasAttributeValuesRecords that have the same AttributeInstanceID value?

Nov 4, 2011 at 5:50 PM

There should not. Anyway, if it was not the case it would throw an exception instead of returning nul, wouldn't it?

Nov 5, 2011 at 9:44 PM

Frankly I don't know as I've only used IRepository's Get() with primary keys. But I would check the uniqueness of the field as if it isn't it would well explain the phenomena.