[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Override a Linq Datacontext Container

Posted on 2010-09-01
13
Medium Priority
?
606 Views
Last Modified: 2012-05-10
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.SomeValue > 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!
0
Comment
Question by:dingir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33579438
Why don't you try writing an extension method?
0
 
LVL 1

Author Comment

by:dingir
ID: 33579524
Please guide me,
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33579587
what exactly you're trying achieve in your code? I may need that to help me understand it correctly before saying further
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:dingir
ID: 33579859
Hi

I just want to continue getting data like

var items = db.Items.Where(d=>d.SomeValue > 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.
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33580335
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.
0
 

Expert Comment

by:nireliyahu
ID: 33588882
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.
0
 
LVL 1

Author Comment

by:dingir
ID: 33593784
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.
0
 

Expert Comment

by:nireliyahu
ID: 33595872
Look i know that the datacontext overide solution is viable just need to search more.
0
 
LVL 1

Author Comment

by:dingir
ID: 33698761
Something more on this topic?
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33698802
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;
        }
    }

Open in new window

0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 1500 total points
ID: 33698812
The other extension Method, in this you're not doing any good though. You're simply hardcoding the filter expression with the extension method which means it won't be flexible.

The original version is far better than this.
public static class DataContextExtension
    {
        public static IList<T> GetItems<T>(this DataContext _dataContext, int _filterValue)
        {
            return (from item in _dataContext.Items.Where(x => x.SomeValue > _filterValue) select item).ToList<T>();
        }
    }

Open in new window

0
 
LVL 1

Author Comment

by:dingir
ID: 33722764
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?
0
 
LVL 1

Author Closing Comment

by:dingir
ID: 33873381
We're both aware that the solution is accurace, but not optimal to the need,
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question