LINQ for compiling cool... but what about dynamic queries at runtime?

just curious how this'll be handled. having queries directly embedded in the code is awesome from a type safety standpoint, but what about at runtime?

i assume some kind of "on-demand" Compiling for a program like "SQL Query Analyzer" (more aptly named LINQ Query Analyzer) that is dynamically compiling these LINQ queries and seeing results, kinda like snippet compiler.

I bring this up becomes sometimes you gotta give query ability to the users (report generation etc)

[477 byte] By [Ensoft] at [2008-2-15]
# 1
This is definitely something we're thinking about. We shipped a sample called Espresso that shows you how to do this yourself. We understand that implementing at a real feature and not just as a sample is a priority for people so this is something we'll be looking at.
JomoFisherMSFT at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 2
Any new thoughts on that?Where I can find this Espresso sample?Thanks AM.
AndreMoraes at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 3
You should take a look at the C# sample named "InteractiveQuery" included with the CTP.
You can also read this blog post to learn more about dynamic queries.
FabriceMARGUERIEMVP at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 4

Fabrice ,

Thanks for the answer.

The sample is a good start but it’s also disappointed, I'm dealing with a Data Warehouse (DW) API, Classes are defined over the DW tool, I don't want to create classes again, the C# sample do a query over one hard coded class:

public class EmployeeView { public string LastName; public string FirstName; public string City; public string Country; public string Extension; public DateTime? BirthDate; }

Then a lot of code to make some expressions… then: var query = db.CreateQuery<EMPLOYEEVIEW>(expression);

I'm also doing interfaces with others systems and data.

I'm not only dealing with the DW Data so I thought that LINQ could be used as one SQL engine to do SQL commands in one in memory Dataset, that brings <> data, all the time, the fields are <> all the time, according with the class that he selected.

Using the DW users could add/remove/edit columns and classes at any time. But using the DW API I can retrieve all the classes and fields available. Also I can build dynamic SQL commands to query the data, and them according with the data retrieved I'll also bring data from several others systems.

Put all together in one DATASET and then I need to QUERY and do LEFT, INNER, Joins, filters, group by over some or all the tables available in the dataset and them GET the RESULTS and show to the users.

I’m not sure if the correct term is Dynamic SQL or not… but what I know is that FOXPRO accept this kind of Dynamic SQL … maybe I just need to forget VB and come back to FoxPRO… I wish that my company will accept this change… worse case I just change company… EASY than expect Foxpro features over .NET.

AndreMoraes at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 5

Andre:

You can do what you want with the new keyword in the query:

var q =
from c in db.Customers
where c.City == "London"
select new { c.CompanyName, c.Phone };

In this case, the results do not need to be a hardcoded class. It's a dynamically created class at runtime (similar to a FoxPro cursor). See the section on Projections in the DLINQ Overview.

DaveFoderick at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 6
How would such a projection work across scopes? (i.e. returning this from a method, a webservice, a remoting call).

I guess the fact that this is basically an instance of a (private) anonymous type, the deserialization won't work at all in any case... so this brings you back to having to define entities/DTOs/view objects over your data... right? Is there any way to use these anonymous projected types as DTOs?

kzu at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 7

class Tuple<A, B>
{
public A ValueA;
public B ValueB;
}

var q =
from c in db.Customers
where c.City == "London"
select new
Tuple { ValueA = c.CompanyName, ValueB = c.Phone };

Only if this would compile... :)

zproxy at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 8

Hi,

There was an easier way in project called COmega which inspired the LINQ and C# 3.0 - for more info look here: http://research.microsoft.com/Comega/doc/comega_whatis.htm, but in COmega you could write something like:

struct{int i; string s;} ReturnAnonymous() {
return new{i=5; s="Hello"};
}

This is very flexible, but I understand that MS doesn't want to allow this in C#, because it can be misused very easilly :-). Using tuple types is still a possible way for returning anonymous types from methods, but since C# syntax for this is a bit cumbersome, it is probably easier to write a class for every type that should be returned.

To use tuples as easy as possible in C# you can define following classes:

// represents tuple type
class
Tuple<A, B>
{
private A _first;
public A First { get { return _first; } set { _first = value; } }
private B _second;
public B Second { get { return _second; } set { _second = value; } }
public Tuple(A first, B second)
{
_first = first; _second = second;
}
}

// contains methods that allows simple construction of tuple types
// using type inferrence (of method parameters) in C#
class Tuple
{
public static Tuple<A,B> New<A,B>(A a0, B a1)
{
return new Tuple<A,B>(a0, a1);
}

public static Tuple<A,Tuple<B,C>> New<A,B,C>(A a0, B a1, C a2)
{
return new Tuple<A, Tuple<B, C>>(a0, New(a1, a2));
}

public static Tuple<A,Tuple<B,Tuple<C,D>>> New<A,B,C,D>(A a0, B a1, C a2, D a3)
{
return new Tuple<A, Tuple<B, Tuple<C, D>>>(a0, New(a1, a2, a3));
}

public static Tuple<A,Tuple<B,Tuple<C,Tuple<D,E>>>> New<A, B, C, D, E>(A a0, B a1, C a2, D a3, E a4)
{
return new Tuple<A, Tuple<B, Tuple<C, Tuple<D, E>>>>(a0, New(a1, a2, a3, a4));
}
}

Now using these classes you can write following code (which compiles fine):

IEnumerable<Tuple<string,string>> q =
from c in db.Customers
where c.City == "London"
select
Tuple.New(c.CompanyName, c.Phone);

Or if you want to return more than two values:

IEnumerable<Tuple<string,Tuple<string,int>>> q =
from c in db.Customers
where c.City == "London"
select
Tuple.New(c.CompanyName, c.Phone, 42);

TomasPetricek at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 9

Having read the items in this section, I’m beginning to wonder “what’s the point?”The idea that you’re ‘looking into’ the topic of dynamic queries is not what I need to hear.

I work with applications for natural resources and in my line of work dynamic queries are a requirement, not an option.A few years ago I wrote a class framework which allows me to build user interfaces which can generate very complex queries by themselves on the basis of user input, usually without additional programming on the application end.All I need do is drop objects into the application, set a few properties and compile.Applications written with that framework have proven to be very successful.

If I needed my data represented as objects, I might see this differently, but to date I have seen no pressing need.It seems there is at least as much room for error in your very complex system as there is in using SQL directly.While Linq appears to have its advantages, the cost seems a lot higher than the benefit.

If your end goal is a true OOP data system which will work with any database system, then I wish you well, because you may be on the right track.Right now, however, this all seems like tail fins on a ‘60s Cadillac.

TPRobinson at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 10

This thread is a bit outdated at this point. We do indeed support dynamic queries. Here's another thread on the topic:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1752078&SiteID=1

Anders

AndersHejlsberg at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 11

Hi Friends i need a help for How to return a query

public List<Int32> GetNewLinkCounter()

{

Int32 lngCurrLinkCtr = 0;

CSAPPayableDataContext appaymentDataContext = new CSAPPayableDataContext();

Table<CSAPLinkCounter> apLinkCtr = appaymentDataContext.GetTable<CSAPLinkCounter>();

var query = from linkCtr in apLinkCtr

select new { linkCtr.LinkCounter };

List<Int32> objList = new List<int>();

foreach (var List in query)

{

objList.Add(List.LinkCounter);

if (objList[0] != null)

{

int LinkCounter = Convert.ToInt32(objList[0]);

}

break;

}

}

Problem is i want to save new LinkCounter in data base then how i can save in data base

Sanjay_ati at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 12

To add data, create a new object, and add it to the table. Then execute SubmitChanges() on the data context:

Code Snippet

var newEmployee = new Employee { Id = 123, Name = "Bob" };

db.Employees.Add(newEmployee);

db.SubmitChanges();

To update, just retrieve the entire object (not just a single property), modify, and execute SubmitChanges():

Code Snippet

var query = db.Employees.Where(e => e.Name.StartsWith("B"));

foreach (var employee in query)

{

employee.Salary = employee.Salary + 1000;

}

db.SubmitChanges();

KeithFarmer at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...

Visual Studio Orcas

Site Classified