Linq to csharp equivalent

  • Thread starter Thread starter d-42
  • Start date Start date
D

d-42

Hi,

I'm trying to under stand linq syntax, and how it relates to 'plain
csharp':
For example, this trivial example grabs the accounts with a name
greater than or equal to "S"...

List<Account> Accounts;

List<Account> mylist = from acc in Accounts where acc.Name >= "S"
select acc.ToList() ;

and it is the same as:

List<Accounts> mylist = Accounts.Where(acc=>acc.Name >="S") .ToList();

But I'm having trouble understanding how to 'translate' more complex
expressions like:

var q = from acc in Accounts
join loc in Locations on acc.AccountID equals loc.AccountID
where loc.IsPrimaryLocation == true
orderby loc.Province descending
select acc;

(Basically the above does an on the fly join with another table,
filters it, and then sorts it by a field in the second table,
ultimately returning the accounts sorted.)

I'd like to translate it out of linq syntax to plain old c#, but I've
gotten over my head; the join in particular is really throwing me. If
someone could translate it I'm hoping I'd be able to see the 'pattern'
that it takes. I'd also appreciate being pointed at any resources that
go over this...

Thanks,
Dave
 
The below is right out of a Reflector Disassemble to C# on
System.Data.Linq.SqlClient.SqlJoin:


internal class SqlJoin : SqlSource
{
// Fields
private SqlExpression condition;
private SqlJoinType joinType;
private SqlSource left;
private SqlSource right;

// Methods
internal SqlJoin(SqlJoinType type, SqlSource left, SqlSource right,
SqlExpression cond, Expression sourceExpression) : base(SqlNodeType.Join,
sourceExpression)
{
this.JoinType = type;
this.Left = left;
this.Right = right;
this.Condition = cond;
}

// Properties
internal SqlExpression Condition
{
get
{
return this.condition;
}
set
{
this.condition = value;
}
}

internal SqlJoinType JoinType
{
get
{
return this.joinType;
}
set
{
this.joinType = value;
}
}

internal SqlSource Left
{
get
{
return this.left;
}
set
{
if (value == null)
{
throw Error.ArgumentNull("value");
}
this.left = value;
}
}

internal SqlSource Right
{
get
{
return this.right;
}
set
{
if (value == null)
{
throw Error.ArgumentNull("value");
}
this.right = value;
}
}
}



-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
 
I'm not quite sure how that gets me any closer to what I'm looking
for.

Looking at the VS2008 intellisense I'm not seeing anything that
corresponds to what you provided. I'm looking at the VS2008
intellisense for Join() on my List<Accounts> and its:

(extension) IQueryable<TResult>
IQueryable<Account>.Join<Account,TInner, TKey, TResult>
(IEnumberable<TInner> Inner,
System.Linq.Expressions.Expression<Func<Account,TKey> >
outerKeySelector,
System.Linq.Expressions.Expression<Func<TInner,TKey> >
innerKeySelector,
System.Linq.Expressions.Expression<Func<TInner,TKey,TResult> >
resultSelector)

and overloads which do it as IEnumberable instead of IQueryable, and
also which let me specify my own IEqualityComparer<TKey> comparer,
presumably if I want a custom equality test.

But frankly, I'm having a hell of time wrapping my head around what it
actually wants.

I did find one example of a Join written as follows:

var q = callLog.Join( contacts,
call => call.Number,
contact => contact.Phone,
(call, contact) => new { contact.FirstName,
contact.LastName,
call.When,
call.Duration })
.Take(5)
.OrderByDescending(call => call.When);

And at least that makes SOME sense... certainly a heck of a lot more
than the intellisense prompt. So for the example problem I'm looking
at...

TInner is List<Location>
TKey is long / Int64 / (SQL bigint)
TResult -- I'm not sure. In the example I found it looks like it
constructs an anonymous type, but I'm not sure what I need for -my-
example problem. I know I don't want an anonymous type at the end but
do I need one here to perform the where and orderby queries in -my-
example problem?

and I presume I don't need to worry about providing my own
IEqualityComparer<TKey>, because long should already have a suitable
one. So that gets me to...

List<Account> Accounts;
List<Location> Locations;

List<Account> mylist = Accounts.Join(
Locations, // this would be TInner
acc => AccountID, // this would be the account outer key
selector
loc=>AccountID, // this would be the locations (tinner)
inner key selector
[ still not sure what I need here though for my initial problem
example ])....nor what follows
....

Definately progress... but not quite a solution...

In any case, I'm not sure how your dissassembly relates to my question
at all.

ALSO, is where are these extension methods defined on msdn... looking
at the intellisense is helpful, but it would be a lot better if I
could find them in a more permanent form (ie that doesn't disappear
when I move my mouse), and ideally with working examples...

All I seem to find is Linq documentation using the C# query syntax,
with only occasional snippets of method syntax.

-best regards,
Dave
 
I'm trying to under stand linq syntax, and how it relates to 'plain
csharp':

My book (C# in Depth) explains query expression expansion, but for a
free alternative which also does so, look at the book preview from
Bruce Eckel and Jamie King:
http://www.mindviewinc.com/Books/CSharp/Index.php
For example, this trivial example grabs the accounts with a name
greater than or equal to "S"...

List<Account> Accounts;

List<Account> mylist = from acc in Accounts where acc.Name >= "S"
select acc.ToList() ;

and it is the same as:

List<Accounts> mylist = Accounts.Where(acc=>acc.Name >="S") .ToList();

Not quite - you'd need brackets round the query expression, otherwise
you're calling
Select(acc => acc.ToList()) which is unlikely to work.
But I'm having trouble understanding how to 'translate' more complex
expressions like:

var q = from acc in Accounts
join loc in Locations on acc.AccountID equals loc.AccountID
where loc.IsPrimaryLocation == true
orderby loc.Province descending
select acc;

(Basically the above does an on the fly join with another table,
filters it, and then sorts it by a field in the second table,
ultimately returning the accounts sorted.)

I'd like to translate it out of linq syntax to plain old c#, but I've
gotten over my head; the join in particular is really throwing me. If
someone could translate it I'm hoping I'd be able to see the 'pattern'
that it takes. I'd also appreciate being pointed at any resources that
go over this...

See the above link, and also the C# 3 unified spec (a search should
find it).

With respect to your extension method queries - I've usually found
that MSDN (locally installed, at least) lists the extension methods
for all classes, and then takes you to the docs for the actual
extension method (e.g. you look up List<T>.Select and it gives the
docs for Enumerable.Select, because the extension method is in the
Enumerable static class).

Jon
 
My book (C# in Depth) explains query expression expansion, but for a
free alternative which also does so, look at the book preview from
Bruce Eckel and Jamie King:http://www.mindviewinc.com/Books/CSharp/Index.php

Thanks for the references. I will look them up.
See the above link, and also the C# 3 unified spec (a search should
find it).

With respect to your extension method queries - I've usually found
that MSDN (locally installed, at least) lists the extension methods
for all classes, and then takes you to the docs for the actual
extension method (e.g. you look up List<T>.Select and it gives the
docs for Enumerable.Select, because the extension method is in the
Enumerable static class).

Aha! Perfect. I was just searching for "linq select" or "linq join"
and getting linq syntax results. Searching for Enumerable.Select,
Enumerable.Join, etc, was just the hint i needed.

Thanks again,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top