473,379 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

SQL to LINQ questions

I have a query which I would like to convert to LINQ. I am having
problems finding good examples or references that cover more complex
queries.
Here is the SQL command I would like to rewrite into C# LINQ. Notice
that one of the returned fields "XrefID" is coming from the second table.
This information is very important but its complicating the query expression
in C# The DataContext has the two classes for the two tables (Customer
and Xref). This particular query needs information from both tables.

Does anyone have any good references that can help me writing this type of
query using LINQ?
Or does anyone know how I would go about dealing with the scenerio I have
discribed here?
select Customer.*,
(select top 1 Xref.XrefID from Xref where Customer.CustomerID =
Xref.CustomerID and Customer.CompanyID = Xref.CompanyID) as XrefID

from Customer

where Customer.CustomerID = 70540 and Customer.CompanyID = 6


--
Thanks for the help,

Dan Tallent
Sep 4 '08 #1
13 1781
"Dan Tallent" <sp**@microsoft.comwrote in message
news:Op**************@TK2MSFTNGP06.phx.gbl...
>I have a query which I would like to convert to LINQ. I am having
problems finding good examples or references that cover more complex
queries.
Here is the SQL command I would like to rewrite into C# LINQ.
Notice that one of the returned fields "XrefID" is coming from the second
table. This information is very important but its complicating the query
expression in C# The DataContext has the two classes for the two
tables (Customer and Xref). This particular query needs information from
both tables.

Does anyone have any good references that can help me writing this type of
query using LINQ?
Or does anyone know how I would go about dealing with the scenerio I have
discribed here?
You should use an anonymous type to aggregate data from records from both
tables:

http://msdn.microsoft.com/en-us/library/bb397696.aspx

Otherwise, the translation of your query should be pretty straightforward.
Of course, you'll need to explicitly list the fields rather than using "*".
Sep 4 '08 #2


"Pavel Minaev" <in****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Dan Tallent" <sp**@microsoft.comwrote in message
news:Op**************@TK2MSFTNGP06.phx.gbl...
>>I have a query which I would like to convert to LINQ. I am having
problems finding good examples or references that cover more complex
queries.
Here is the SQL command I would like to rewrite into C# LINQ. Notice that
one of the returned fields "XrefID" is coming from the second table. This
information is very important but its complicating the query expression
in C# The DataContext has the two classes for the two tables
(Customer and Xref). This particular query needs information from both
tables.

Does anyone have any good references that can help me writing this type
of query using LINQ?
Or does anyone know how I would go about dealing with the scenerio I have
discribed here?

You should use an anonymous type to aggregate data from records from both
tables:

http://msdn.microsoft.com/en-us/library/bb397696.aspx

Otherwise, the translation of your query should be pretty straightforward.
Of course, you'll need to explicitly list the fields rather than using
"*".
I plan on using anonymous types like the example you provided, but even in
the example you provide it does not show a join.

The code I was playing with looks like this:
//------------------------------------------------------------------------

var QueryCustName =

from cust in DC.Customers

join xref in DC.Xrefs

on cust.CustomerID equals xref.CustomerID

where cust.CompanyID = xref.CompanyID

where cust.CompanyID = 6

select new { cust.DisplayName, cust.Address1, xref.XrefID } ;

//------------------------------------------------------------------------

The problem is I would prefer to use the following syntax, but I am unsure
of how to do a join using this method. All of the examples I have found
look like the one above.

var QueryCustName =

from Cust in DC.Customers

..OrderBy(Cust =Cust.DisplayName)

..Where(Cust =Cust.CompanyID == mCompanyID)

..Take(50)

select new {Cust.DisplayName, Cust.Address1, Xref.XrefID };

//------------------------------------------------------------------------

any help would be appreciated.

Thanks

Dan





Sep 4 '08 #3
On Sep 4, 6:38*pm, "Dan Tallent" <s...@microsoft.comwrote:
I plan on using anonymous types like the example you provided, but even in
the example you provide it does not show a join.

The code I was playing with looks like this:
//------------------------------------------------------------------------

var QueryCustName =

from cust in DC.Customers

join xref in DC.Xrefs

on cust.CustomerID equals xref.CustomerID

where cust.CompanyID = xref.CompanyID

where cust.CompanyID = 6

select new { cust.DisplayName, cust.Address1, xref.XrefID } ;
If you are doing an equijoin on a complex key, it's better to do both
comparisons in the join - note that you can use anonymous types for
complex keys in such scenarios, i.e.:

join xref in DC.XRefs on new { cust.CustomerID, cust.CompanyID }
equals new { xref.CustomerID, xref.CompanyID }
//------------------------------------------------------------------------

The problem is I would prefer to use the following syntax, but I am unsure
of how to do a join using this method. *All of the examples I have found
look like the one above.

var QueryCustName =

from Cust in DC.Customers

.OrderBy(Cust =Cust.DisplayName)

.Where(Cust =Cust.CompanyID == mCompanyID)

.Take(50)

select new {Cust.DisplayName, Cust.Address1, *Xref.XrefID };

//------------------------------------------------------------------------

any help would be appreciated.
The obvious question is - what's wrong with the first code snippet?
Joins (both implicit and explicit) are one case where LINQ syntactic
sugar is usually much clearer than chained method calls.

However, if you insist on doing it the other way, then you just
translate LINQ join to Enumerable.Join:

DC.Customers
..Join(
DC.Xrefs,
cust =new { cust.CustomerID, cust.CompanyID },
xref =new { xref.CustomerID, xref.CompanyID },
(cust, xref) =new { cust.DisplayName, cust.Address1,
xref.XRefID })
..OrderBy(r =r.DisplayName)
..Take(50)

Sep 4 '08 #4
This looks really great! I'll start playing with it here in a minute.
I am still trying to understand how the code works, mainly because I could
not figure out how to use the .Take() function without using dot notation.

One more question... if I anonymous types, how would a DataGridView know
what the primary key is ? (for updating, deleting, etc)

Thanks
Dan


"Pavel Minaev" <in****@gmail.comwrote in message
news:c0**********************************@t1g2000p ra.googlegroups.com...
On Sep 4, 6:38 pm, "Dan Tallent" <s...@microsoft.comwrote:
I plan on using anonymous types like the example you provided, but even in
the example you provide it does not show a join.

The code I was playing with looks like this:
//------------------------------------------------------------------------

var QueryCustName =

from cust in DC.Customers

join xref in DC.Xrefs

on cust.CustomerID equals xref.CustomerID

where cust.CompanyID = xref.CompanyID

where cust.CompanyID = 6

select new { cust.DisplayName, cust.Address1, xref.XrefID } ;
If you are doing an equijoin on a complex key, it's better to do both
comparisons in the join - note that you can use anonymous types for
complex keys in such scenarios, i.e.:

join xref in DC.XRefs on new { cust.CustomerID, cust.CompanyID }
equals new { xref.CustomerID, xref.CompanyID }
//------------------------------------------------------------------------

The problem is I would prefer to use the following syntax, but I am unsure
of how to do a join using this method. All of the examples I have found
look like the one above.

var QueryCustName =

from Cust in DC.Customers

.OrderBy(Cust =Cust.DisplayName)

.Where(Cust =Cust.CompanyID == mCompanyID)

.Take(50)

select new {Cust.DisplayName, Cust.Address1, Xref.XrefID };

//------------------------------------------------------------------------

any help would be appreciated.
The obvious question is - what's wrong with the first code snippet?
Joins (both implicit and explicit) are one case where LINQ syntactic
sugar is usually much clearer than chained method calls.

However, if you insist on doing it the other way, then you just
translate LINQ join to Enumerable.Join:

DC.Customers
..Join(
DC.Xrefs,
cust =new { cust.CustomerID, cust.CompanyID },
xref =new { xref.CustomerID, xref.CompanyID },
(cust, xref) =new { cust.DisplayName, cust.Address1,
xref.XRefID })
..OrderBy(r =r.DisplayName)
..Take(50)
Sep 4 '08 #5
Anonymous types....

If the LINQ query is returning an anonymous type, how can you pass this
object outside of your function / class ?

In my prior example I have a DC.Customers object class. When I pass this
to another function it is of type Customer.
This lets me write code in a function like:

//----------------------------------------------------------------------------------------------------------------
Object SelectedRecord = dataGridView1.SelectedRows[0].DataBoundItem;

RecordID = STC.GetRecordID(SelectedRecord);

//----------------------------------------------------------------------------------------------------------------

The class STC is actually a derived class. I have several different STC
type classes which work with different data (Customers, Inventory, Invoices,
etc)
They all have a function called GetRecordID which is expecting a Object

public override int GetRecordID(object SelectedRecord)

{

int RecordID = 0;
if (SelectedRecord.GetType() == typeof(Search.Customer))

{

Customer SelectedCust = (Customer)SelectedRecord;

RecordID = SelectedCust.CustomerID;

}
return RecordID;

//----------------------------------------------------------------------------------------------------------------

What would be the correct method of achieving this if I'm using anonymous
types?

Thanks again
Dan


Sep 4 '08 #6
On Sep 4, 10:03*pm, "Dan Tallent" <s...@microsoft.comwrote:
This looks really great! *I'll start playing with it here in a minute.
I am still trying to understand how the code works, *mainly because I *could
not figure out how to use the .Take() function without using dot notation..
You can't. However, since it usually comes last, you can enclose the
entire LINQ query into parentheses, and then apply Take() to it.
One more question... if I anonymous types, how would a DataGridView know
what the primary key is ? * (for updating, deleting, etc)
DataGridView doesn't deal with primary keys. When bound to an
updatable data source, it just treats it as a collection, and, if
possible, as an IBindingList.
Sep 4 '08 #7
On Sep 4, 10:32*pm, "Dan Tallent" <s...@microsoft.comwrote:
Anonymous types....

If the LINQ query is returning an anonymous type, how can you pass this
object outside of your function / class ?
You can't. You'll have to declare a named class to hold your values if
you want to have a function with such a return type.
Sep 4 '08 #8
You can't. You'll have to declare a named class to hold your values if
you want to have a function with such a return type.
Although an anonymous type can't be on the method signature, that
isn't /entirely/ true, in 2 different ways. The most common way of
passing an anonymous type out is via generics - but of course the
method you call won't know a lot about the anonymous type (other than
"look, there are some Ts..."). But also, you can still pass data
around as object, or List<object(or IList) etc. This might not sound
very useful, but it is: it means you can use an anonymous type as a
DataSource for data-bound UI (such as a DataGridView etc).

Marc
Sep 4 '08 #9
I was looking into reflection to see how I could get one of the values from
the object. The object
was created by using the LINQ query as an anonymous type. I am just trying
to recover the value
of one of the fields within the object. I have been successful getting a
list of fields, but not getting the actual values.

I would not have a problem creating a class so I could simply cast this
object out, but I don't know how to create a LINQ query to use a user
defined class.
Keep in mind the result of my query is pulling data from two separate tables
so I can't simply use one of the classes in the datacontext.
public override int GetRecordID(object SelectedRecord)

{

int RecordID = 0;

Type objectType = SelectedRecord.GetType();

foreach (System.Reflection.MemberInfo MI in objectType.GetMembers())

{

Console.WriteLine(MI.Name);

}

}

Any help is greatly appreciated

Thanks

Dan




"Marc Gravell" <ma**********@gmail.comwrote in message
news:84**********************************@e53g2000 hsa.googlegroups.com...
>You can't. You'll have to declare a named class to hold your values if
you want to have a function with such a return type.

Although an anonymous type can't be on the method signature, that
isn't /entirely/ true, in 2 different ways. The most common way of
passing an anonymous type out is via generics - but of course the
method you call won't know a lot about the anonymous type (other than
"look, there are some Ts..."). But also, you can still pass data
around as object, or List<object(or IList) etc. This might not sound
very useful, but it is: it means you can use an anonymous type as a
DataSource for data-bound UI (such as a DataGridView etc).

Marc

Sep 4 '08 #10
PropertyInfo and GetValue:

using System;

class Program
{
static void Main()
{
object obj = GetSomeObject();
foreach (var prop in obj.GetType().GetProperties())
{
Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(obj,
null));
}
}
static object GetSomeObject()
{
return new { Foo = 1, Bar = "abc" };
}
}

Sep 4 '08 #11
but I don't know how to create a LINQ query to use a user
defined class
The same as you do to an anon-type. LINQ doesn't even know about
anonymous types:

public class Test {
public string Foo {get;set;}
public int Bar {get;set;}
}
....
select new Test {Foo = x.SomeProp, Bar = y.SomeOtherProp};

The only caveat is that the data-context won't usually let you create
instances of objects that *it* thinks it controls - i.e. if you have a
SomeDataRecord type in your LINQ context, you can't use "select" to
create a new SomeDataRecord manually - you need to just let the data-
context create it.

Marc
Sep 4 '08 #12
LINQ doesn't even know about anonymous types:

By which I mean that Expression / LINQ-to-SQL etc don't have any
specific knowledge of them - just that an object of some Type should
be created. Obviously anonymous types are a key part of the overall
LINQ design at the caller...

Marc
Sep 4 '08 #13
Great example, thanks. I was able to use this to get the information I
needed.

Thanks again
Dan

"Marc Gravell" <ma**********@gmail.comwrote in message
news:67**********************************@34g2000h sh.googlegroups.com...
PropertyInfo and GetValue:

using System;

class Program
{
static void Main()
{
object obj = GetSomeObject();
foreach (var prop in obj.GetType().GetProperties())
{
Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(obj,
null));
}
}
static object GetSomeObject()
{
return new { Foo = 1, Bar = "abc" };
}
}

Sep 5 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shapper | last post by:
Hello, I have been reading a few articles about LINQ and I have a few questions: 1. What do I need to start using it in my ASP.NET 2.0 / SQL 2005 / Visual Studio 2005 web sites? 2. Is...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
1
by: Frederik | last post by:
Hi all, Am I correct when stating that LINQ replaces somewhat DataTables? I have done some reading concerning LINQ, but I'm still puzzled as to whether I should use LINQ or not. My application...
1
by: Ilyas | last post by:
Hi all I have a database that contains several tables. Each table has the name data01, data02, data03 etc The column definitions for each table all contains several standard columns and then...
2
by: stephen | last post by:
Hi, I apologize for posting Linq questions here. I wanted to try to work with Linq and found a Linq (May 2006) CTP that can be used with .NET 2005. when I run the msi, i get this message: "A...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
2
by: shapper | last post by:
Hello, I have the following Linq query: List<PostsTaginsert = (from t in (from t in database.Tags join p in paper.Tags on t.Name equals p.Name select t).ToList() select new PostsTag {
4
by: CSharper | last post by:
I have following XML <root> <Person id="1"> <Name>a</Name> </Person> <Person id="2"> <Name>b</Name> </Person> </root>
4
by: George | last post by:
I am a bit conservative type and usually give some time for technology to mature before starting to try it. Today my question is Linq. To start using it or not. So here is the voting questions....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.