LINQ - 100 Views - Same Columns - Generic Queries

Here is my scenario: We have a legacy system that has about 100 views that all pull the same columns worth of data.

Now, in my DataContext I have all the views in the context and I have a seperate query from each one. Each query's results loads into a single List that gets returned to the application.

Is it possible to have a single query that I can pass in a object to know which table to pull from?

Example:

var query = from GenericTable.Where(whereClause).Select(ObjectMap);

Note: i know this is not the right syntax, it is just for example only.

My main goal is to avoid having to write 100 different queries when they are all the same thing, just pointed to a different view each time.

Any suggestions are welcome, even if it is to keep the 100 queries.

Thanks!

--------------Solutions-------------

Create a common interface for all of the mapping types that were generated by linq to sql. Then use partial class definitions to add the interface to all the classes.

Then write your 100 queries like this:

public IQueryable<T> GetQueryAgainst<T>(IQueryable<T> source,
string search) where T : IMyData
{
IQueryable<T> result = source.Where(t => t.Name.Contains(search));
return result;
}

and call it like:

List<Car> cars = GetQueryAgainst(dc.Cars, "Bob").ToList();
List<People> people = GetQueryAgainst(dc.People, "Bob").ToList();
List<Orders> orders = GetQueryAgainst(dc.Orders, "Bob").ToList();

One approach may be using Dynamic Linq. Then you can specify the where clause and the select using strings.

var genericList = GetView("predicate to identify view");
genericList.Where("field1 = @1", value1).select("Field1, Field2");

Category:c# Time:2010-02-26 Views:0
Tags: c# linq generics

Related post

  • Rails find_by_sql - how to run generic queries 2009-12-20

    I need to execute this query to find out the next auto_increment value that will be used by MySQL for a particular table. find_by_sql ["select auto_increment from information_schema.tables where table_schema = ? and table_name = ? and auto_increment

  • How do I apply to all sub folders the main folder's view's columns widths? 2013-05-29

    folder view column width sub folders --------------Solutions------------- Hi Mike, Thank you for the update. If you want to have the same type of view for all the folders then open any folder in the particular view type and then follow the steps give

  • Copy SQL 2005 view result column headers? 2010-03-12

    Is there a way to copy View output column headers along with the data? There is a setting in Options to include column headers with query results, but that only works with "New Query" and Stored Procedure output. Looks like SSMS 2008 has this functio

  • Getting View's Column Description 2010-09-22

    Is it a way to retrieve the view's column description from the base table (fn_listextendedproperty)? The following syntax only list down the column name from the view SELECT * FROM sys.columns c JOIN sys.views v on c.OBJECT_ID = V.OBJECT_ID JOIN sys.

  • how to use parameter in linq to select different columns 2010-10-08

    i want to use linq to select certain columns to populate each combobox. right i have individual linq query to do the job. i wish to write a method to do that. var getUserName = Entity.Select(a=>a.Username); var getType = Entity.Select(a=>a.Type

  • Android how to sort list of objects in list view by column 2011-09-20

    I bind list of class objects using adapters in android list view, the list view have 3 column headers(3 header buttons), each header have click event, now i want to sort list view by column means when ever i click on first header column, the data sor

  • Make Table Width 100% with Last Column Filling Remainder without squashing other columns content/width 2011-10-01

    Is it possible to make a table (which will have dynamically created text) to have for example: table width 100% of the screen 5 columns last column is empty ( ) the first four columns have text and their width is dynamic the last column is remainder

  • INSERT with Linq omitting some "fake" columns 2011-10-06

    I have a table in the database with the following columns: ID, Name, Txt. We are using Linq To Sql to implement our DAL. In there another collegue added two extra columns so in the code the same table results: ID, Name, Txt, NameTemp, TxtTemp. These

  • view 1 column value 3 times using where clause 2012-02-17

    is it possible to view 1 column value 3 times using where clause.. for eg select Lastname from Emplyee where firstname = 'John' , 'Mike' , 'Harry' It should return values like firstname lastname John Moraes Mike Tan Harry Potter Thanx in advance ----

  • MS Word 2007 is confused by definition of 100% View 2013-09-24

    I notice that MS Word 2007 cannot display documents in "Full Screen" at 100% Unlike Word 2003 that can perform this fundamental task perfectly I appears that Word 2007 is confused between "100%" (document view 'as is') and "Full Page" view (100% of d

  • Why isn't my word document page 8.5 inches even though it is at 100% view? 2014-03-12

    I selected page as letter. So my understanding is that at 100% viewing, the page on the screen should be 8.5 inches wide. But it is NOT! Why is that? I need an answer, please! Thanks! --------------Solutions------------- 100% zoom does not mean that

  • pages I work on keep opening up at 100% view after i change them to 70% and save 2014-03-12

    I am having a problem with excel 2010. That the pages I work on keep opening up at 100% view after i change them to 70% and save. Workbook also open on different tabs and not thae last tab I worked on. I am the only that has access to these files. Ho

  • LINQ-to-Entities, Ambiguous Column Name with association between two views with the same column name 2012-02-08

    I am just getting into Entity Framework for the first time beyond simple examples. I am using the model-first approach and am querying the data source with LINQ-to-Entities. I have created an entity model that I am exposing as an OData service agains

  • C# LINQ to SQL: Refactoring this Generic GetByID method 2009-04-09

    I wrote the following method. public T GetByID(int id) { var dbcontext = DB; var table = dbcontext.GetTable<T>(); return table.ToList().SingleOrDefault(e => Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e, null)) == id); } Bas

  • SQL: View against Table - are queries against the View still using Table Indexes? 2009-06-01

    I'm using a view ("UsersActive") against my table ("Users"). The view has only one filter, it checks if DateTime Users.DeletedOn is NULL; it basically contains all Users that are not deleted. If I now execute Linq queries against the view instead of

  • Linq: Querying a "Virtual" columns created from one split column 2010-09-24

    I'm working with a pretty nasty looking, but non-changable db. We've got a table, called "Locations" with column, "Position" This column has values like "A SHELF 5" Which means "Case A, Shelf 5". In an ideal world, I'd have a Case and Shelf column, w

  • Is there a LINQ way of converting a generic list of objects to an X by Y matrix of the same type? 2011-04-17

    I have a generic list of objects, for example: List<Photo> and want to convert this to something I can use on an MVC view. In the form (basic example): <% foreach (var row in PhotoList) { %> // Render Matrix (using photo properties) <t

  • linq to entity Contains() and nested queries 2009-06-30

    i've a trouble with linq, i'll explain on example : i have a database table called Employee which got FirstName and LastName columns, and a method to search employees which gets a nameList list as argument, the elements in this list are names formatt

  • LinQ XML mapping to a generic type 2009-08-24

    I´m trying to use an external XML file to map the output from a stored procedure into an instance of a class. The problem is that my class is of a generic type: public class MyValue<T> { public T Value { get; set; } } Searching through a lot of

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.687 (s). 13 q(s)