This is a migrated thread and some comments may be shown as answers.

Generic LINQ DAL

5 Answers 74 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joebet
Top achievements
Rank 1
Joebet asked on 19 Jul 2010, 10:43 AM
Good day guru. Im having trouble in manipulating data using Telerik ORM with LINQ. What I am trying to accomplish is to make a dynamic or generic LINQ that will access my database. that is...

var result = from o in scope.Extent<ENTITY>()
             where o.Customer.Contains("John")
             select o;
scope.Transaction.Begin();
scope.Remove(result.ToList());
scope.Transaction.Commit();
scope.Dispose();

I need to make the above code a function that is reusable to all programmer by just supplying the above capitalized "ENTITY" which corresponds to the actual entity properties generated by telerik. ie. Product, Customer, Supplier or anything the user wants.. 

Thanks...

5 Answers, 1 is accepted

Sort by
0
Jan Blessenohl
Telerik team
answered on 19 Jul 2010, 11:44 AM
Hello Joebet,
You can use our own string based query langauge OQL or you can use dynamic Linq where you use Linq syntax but with strings.

Best wishes,
Jan Blessenohl
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joebet
Top achievements
Rank 1
answered on 20 Jul 2010, 03:21 AM
Do OQL have the CRUD functionality? because the documentation only shows the "Select" statement. Also i read the tutorial links your provided but what I am trying to accomplish is indeed a dynamic linq but with the actual entity being the parameter all that you had given was making a dynamic where and order by linq and the entity to be referred was predefined. Sorry for my bad english im not really good at explaining things. but is it clear? thanks and im hoping for a swift reply.
0
Jan Blessenohl
Telerik team
answered on 20 Jul 2010, 08:14 AM
Hello Joebet,
Do you want to do the delete as part of the query? That is indeed not possible. Both query languages are only meant for reading data. You have to stay with your code or use an additional ADO connection to do such operations. ORMs are by design bad in mass operations and if your list, that you want to delete, is bigger I count this as such.

Best wishes,
Jan Blessenohl
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Shawn Krivjansky
Top achievements
Rank 1
answered on 09 May 2012, 01:28 AM
One of the original questions was asked twice, but still never answered...

Can the ENTITY (i.e. the TABLE NAME) in a LINQ/Lambda query be dynamic?
Yes, we know stuff inside the WHERE, ORDERBY, etc... can be dynamic.

Example:
dbContext.DYNAMICENTITYs.Where(Function(db) db.Code = "123").ToList

Can the "DYNAMICENTITY" above be made generic/dynamic?

For example...
Let's say you have 100 entities/tables with the same structure... different entity/table names.
You want to use ONE LINQ/Lambda statement to query (like above).

Can you use a STRING value in some way to define one of the 100 tables/entities, but use ONE LINQ/Lambda query??

0
Thomas
Telerik team
answered on 09 May 2012, 01:46 PM
Using an interface over your common persistent properties could be used:

IQueryable<T> FilterName<T>(string name) where T : IMyNamedIfc
{
    return context.GetAll<T>().Where(x => x.Name.Contains(name));
}

interface IMyNamedIfc
{
    stirng Name { get; }
}

class MyPersistentClassA : MyNamedIfc
{
    public string Name { get; set; } // persistent
}

class MyPersistentClassB : MyNamedIfc
{
    public string Name { get; set; } // persistent
}

Then a
var resultA = FilterName<MyPersistentClassA>("hello").ToList();
var resultB = FilterName<MyPersistentClassB>("world").ToList();
should be possible.

The other option includes using dynamic LINQ.

Regards,
Thomas
the Telerik team
Follow @OpenAccessORM Twitter channel to get first the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
Tags
LINQ (LINQ specific questions)
Asked by
Joebet
Top achievements
Rank 1
Answers by
Jan Blessenohl
Telerik team
Joebet
Top achievements
Rank 1
Shawn Krivjansky
Top achievements
Rank 1
Thomas
Telerik team
Share this question
or