i founf a half solution for my problem in that post: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/
Let me try to explain and illustrate my problem.
C#
public class ClassEntityA
{
public int Id { get; set; }
public string Text { get; set; }
}
public class ClassEntityB
{
public int Id { get; set; }
public string Text { get; set; }
public Nullable EntityA { get; set; }
}
// I need to do the following query in Ado Entity. That query works in Linq To Sql, but not suported in Linq To Entity
listint> list = new Listin>() { 1, 2, 3 };
var itens = DbContext.ClassEntityB.Where(c => (list.Contains(c.EntityA.Id) == false || c.EntityA == null);
// i Found a Half Solution to do that in Linq To Entity doing that:
// But Here i can do the second test (OR c.EntityA == null). How can i do That?
listint> list = new Listin>() { 1, 2, 3 };
var itens = DbEntities.ClassEntityB.WhereNotIn(c => c.EntityA.Id, list.AsEnumerableint>())
#region Where Not In
private static Expressionbool>>
GetWhereNotInExpression(
Expression> propertySelector,
IEnumerable values)
{
ParameterExpression p =
propertySelector.Parameters.Single();
if (!values.Any())
{
return e => true;
}
var unequals = values.Select(value =>
(Expression)Expression.NotEqual(
propertySelector.Body,
Expression.Constant(value, typeof(TValue))
)
);
var body = unequals.Aggregate(
(accumulate, unequal) => Expression.And(accumulate, unequal));
return Expression.Lambdabool>>(body, p);
}
public static IQueryable
WhereNotIn(this IQueryable source,
Expression> propertySelector,
params TValue[] values)
{
return source.Where(GetWhereNotInExpression(
propertySelector, values));
}
public static IQueryable
WhereNotIn(this IQueryable source,
Expression> propertySelector,
IEnumerable values)
{
return source.Where(GetWhereNotInExpression(
propertySelector, values));
}
#endregionCan any of you help me?
i need to do a Query in the Ado that have a ("Where not in" or "Property == null) test.
thx.
Luciano Correia

Piotr Justyna70 reputationPosted Nov 28, 2009, 1:19 PM EST
I get your point. It's rather difficult to manipulate the 'OR' operator, but don't worry since we have De Morgan's laws.
Using them you can instead of:
Where is true: Item is not in collection OR item is null
write this:
Where is not true: Item is in collection AND item is not null
This is a lot easier, because you can write your LINQ query like this (you will just have to add an expression 'WhereIn'):
Now you just have to nagate the obtained set of records:
Or to save some space:
method GetWhereNotInExpression. If you do so, you can with no problem access each value's properties and determine if they're null or not. This seems to be a better solution, because you have only one query in this case.The second solution is to avoid using generic type in 'values' collection in your
I hope this helps - if it does please mark it as helpful :)
Cheers!
Luciano Correia8 reputationPosted Nov 30, 2009, 4:32 AM EST
very good idea, thx alot. but i got another problem:
public class Empresa { public int Id { get; set; } public string Text { get; set; } } public class Cliente { public int Id { get; set; } public string Text { get; set; } public Empresa Emp { get; set; } } class Program { public static List empresas = new List(); public static List clientes = new List(); static void Main(string[] args) { LoadData(); List deniedAccess = new List() { 1, 2 }; var itens = clientes.AsQueryable().WhereNotIn(c => c.Emp.Id, deniedAccess); foreach (var item in itens) { Console.WriteLine(string.Format("Name: {0}", item.Text)); } Console.ReadKey(); } private static void LoadData() { empresas.Add(new Empresa() { Id = 1, Text = "Empresa 1" }); empresas.Add(new Empresa() { Id = 2, Text = "Empresa 2" }); empresas.Add(new Empresa() { Id = 3, Text = "Empresa 3" }); empresas.Add(new Empresa() { Id = 4, Text = "Empresa 4" }); clientes.Add(new Cliente() { Id = 1, Text = "Cliente 1", Emp = empresas.Find(c => c.Id == 1) }); clientes.Add(new Cliente() { Id = 2, Text = "Cliente 2", Emp = empresas.Find(c => c.Id == 2) }); clientes.Add(new Cliente() { Id = 3, Text = "Cliente 3", Emp = empresas.Find(c => c.Id == 3) }); clientes.Add(new Cliente() { Id = 4, Text = "Cliente 4", Emp = empresas.Find(c => c.Id == 4) }); clientes.Add(new Cliente() { Id = 5, Text = "Cliente 5", Emp = null }); } }In that example i got an Exception because the record "Cliente 5" has the Emp field as null.
So, any other idea?
Piotr Justyna70 reputationPosted Nov 30, 2009, 5:54 AM EST
Could you paste the whole exception you are getting? I was convinced, that there should be no errors since I'm checking if the object is not null. Could you also paste your expression code?
Cheers!
Luciano Correia8 reputationPosted Nov 30, 2009, 7:37 AM EST
i got a "NulReferenceException" because when i try c => c.Emp.ID emp is Null
The second example, shows exactly what i need
Piotr Justyna70 reputationPosted Dec 1, 2009, 12:52 AM EST
I'm sorry for a late response. You're right, that complicates your problem a bit. But don't worry - the quickest solution I can think of is:
1) Get all B entities having A entity null
2) Get all A entities having A entity not null
3) Get all A entities which are not in your list querying a collection from point 2).
4) Merge collections from points 1 and 3 (using Concat method or List
Does it work now?
Cheers!
Luciano Correia8 reputationPosted Dec 1, 2009, 3:15 AM EST
the Ado Data Service does not suporte "Union"
but thx alot man..
Piotr Justyna70 reputationPosted Dec 1, 2009, 3:26 AM EST
I'll try to think how to optimize this, but since your problem is solved please mark it as helpful.
Luciano Correia8 reputationPosted Dec 1, 2009, 4:37 AM EST
The problem as not soled, because the Ado Data Service does not suporte "Union"