dingir
asked on
Override a Linq Datacontext Container
I would like to have a sort of override on a specific class of a linq-2-sql.
Like when fetching data from that class, automatically use a filter.
Something.DataContext db = new Something.DataContext();
var items = db.Items.Where(d=>d.SomeVa lue > 10);
Which gave me a resultset to items.
The problem is, that I have rows in the items-table that reminds of a "visible = false" or a "validUntilDate < date" that I want to take care of without have to specify it everytime i use that class in the datacontext.
I will remember that there are possible to create a sort of public partial class that inherits the datacontext and add something to it, which will be refered on use from elsewhere. Though, i'm not sure how to format and where to place it.
Some help would be nice!
Like when fetching data from that class, automatically use a filter.
Something.DataContext db = new Something.DataContext();
var items = db.Items.Where(d=>d.SomeVa
Which gave me a resultset to items.
The problem is, that I have rows in the items-table that reminds of a "visible = false" or a "validUntilDate < date" that I want to take care of without have to specify it everytime i use that class in the datacontext.
I will remember that there are possible to create a sort of public partial class that inherits the datacontext and add something to it, which will be refered on use from elsewhere. Though, i'm not sure how to format and where to place it.
Some help would be nice!
Why don't you try writing an extension method?
ASKER
Please guide me,
what exactly you're trying achieve in your code? I may need that to help me understand it correctly before saying further
ASKER
Hi
I just want to continue getting data like
var items = db.Items.Where(d=>d.SomeVa lue > 10);
and feel safe that there are an underlying type/method that filter out the unwanted records,
which means something the above codes in princip rans on already filtered data, but you wouldnt recognize it.
I just want to continue getting data like
var items = db.Items.Where(d=>d.SomeVa
and feel safe that there are an underlying type/method that filter out the unwanted records,
which means something the above codes in princip rans on already filtered data, but you wouldnt recognize it.
The Linq pattern will only allow you to write Query like expression. In this case the Where clause will help you to exclude unwanted objects. I don't think there's other way to do it.
Hi,
i have a solution for you:
put in your class variable
public IQueryable<Item> FilteredItems;
and in your class ctor put the filter u want :
Something.DataContext db = new Something.DataContext();
FilteredItems = db.Items.Where(i => i.visible == false || validUntilDate < date);
and than wrote youre quiry from FilteredItems
var items = FilteredItems .Where(d=>d.SomeValue > 10);
with this solution you create any filter you want.
i have a solution for you:
put in your class variable
public IQueryable<Item> FilteredItems;
and in your class ctor put the filter u want :
Something.DataContext db = new Something.DataContext();
FilteredItems = db.Items.Where(i => i.visible == false || validUntilDate < date);
and than wrote youre quiry from FilteredItems
var items = FilteredItems .Where(d=>d.SomeValue > 10);
with this solution you create any filter you want.
ASKER
Hi
Thanks. That will probably done it but still affect the way i / someone suspect to use the datacontext.
I feel there must be a way to put an override or psrtial class that being inherited by the datacontext class.
Maybe the problem lies in your answer. U can't, but can create a method that making it feel mostly like that.
Maybe a sort of db.items and a db.itemsfiltered. maybe also set the items datacontext class to private.
Thanks. That will probably done it but still affect the way i / someone suspect to use the datacontext.
I feel there must be a way to put an override or psrtial class that being inherited by the datacontext class.
Maybe the problem lies in your answer. U can't, but can create a method that making it feel mostly like that.
Maybe a sort of db.items and a db.itemsfiltered. maybe also set the items datacontext class to private.
Look i know that the datacontext overide solution is viable just need to search more.
ASKER
Something more on this topic?
This is kind of Extension Method I was talking about. But like you see it's not straight forward and flexible too
public static class ListExtension
{
public static IList<T> Where<T>(this IList _list, int _filterValue)
{
IList<T> list = new List<T>();
foreach (T item in _list)
{
if (item.SomeValue > _filterValue)
list.Add(item);
}
return list;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Your probably right. There might be a solution / several but no one of them doin any good with the drawbacks in mind.
I understand as that I need to use a stored procedure or database-stored view giving this full functionality?
I understand as that I need to use a stored procedure or database-stored view giving this full functionality?
ASKER
We're both aware that the solution is accurace, but not optimal to the need,