views:

76

answers:

3

I'm trying to wrap my head around linq -> nhib

I have a simple bit of sql that i'm trying to get working in nhibernate.linq

 select * from 
 ColModel where ColModel.DataIndex 
 not in ('CostElement1', 'CostElement2', 'CostElement3')
 and ColModel.ReportId = 1

The list of excluded DataIndex values comes in in the form of a List<string> called excludeNames

Here is what I have tried but it seems that it's not really feeling the love:

var s = base._sessionManager.OpenSession();

var query = from col in s.Linq<ColModel>()
            where col.Report.Id == reportid &&
            !(from c in s.Linq<ColModel>() select c.DataIndex).Contains(excludeNames)
            select col;

return query.ToList();

the error:

The type arguments for method 'System.Linq.Enumerable.Contains<TSource>(System.Collections.Generic.IEnumerable<TSource>, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I'm pretty sure I'm borfing this from the offset so any pointers would be well appreciated :)

w://

A: 

I think you have your exclusion backwards.

s = base._sessionManager.OpenSession();

var query = from col in s.Linq<ColModel>()
            where col.Report.Id == reportid &&
                  !excludeNames.Contains(col.DataIndex)
            select col;

return query.ToList();

Collection.Contains(item) will produce the SQL item in (...collection...), adding the negation will get you what you want.

tvanfosson
A: 

Contains doesn't accept a list.

There are ways to work around this in LINQ, but I'm not sure which, if any, of those will work in NH Linq

Jason Dentler
A: 

responding doesn't allow for code so have to add response here :s


thanks!!

nearly got it but the sql now comes out as this:

FROM [ColModel] this_ left outer join [Report] report1_ on this_.Id=report1_.Id WHERE (report1_.Id = 1 and not ((this_.DataIndex in ('CostElement2', 'CostElement3', 'CostElement4'))))

of course

this_.Id=report1_.Id

should be

this_.ReportId = report1_.Id

the mapping for ColModel has this:

HasOne(x => x.Report);

and the mapping for Report has this:

HasMany(x => x.ColModel) .Cascade.All();

any ideas why it would try to link the colmodel.id to the report id?

cvista
ffr - the issue was in the mapping - i should be using References not HasOneReferences(x => x.Report, "ReportId");
cvista