What Should I know to underestand LINQ better? - c#

As LINQ is a good query language in dot net and everyone should be able to work with it.
What are the necessary abilities which a programmer should have, before start learning LINQ.
And after that, What should he know about LINQ? (important tips)

What kind of Linq are you interested in - Linq to Objects, Linq to SQL, implementing your own Linq provider ?
My advice is that you should first learn about the features that Linq is based upon :
IQueryable and IEnumerable extension methods
Method chaining and deferred execution
Anonymous methods and lambda expressions
Linq query syntax
Expression trees (if you aim at implementing your Linq provider)
Then in turn these features rely on more basic concepts :
Generics
Delegates
Collections
A couple of books that could help you :
C# in depth (J. Skeet) - great book to learn C#
LINQ in Action (Marguerie, Eichert, Wooley)

A decent understanding of Closures / Lambda, IEnumerable, Generics, Expression Trees and Method Chaining, would be a good start. LINQ is pretty much just a utility set based upon that.
But you can definitely learn to utilize LINQ without a solid understanding of each and every single feature mentioned.

If you want to understand the principles behind LINQ than it is very useful to learn some basics of functional programming, because LINQ is largely based on this paradigm. Of course, you don't need that to use LINQ in practice, but it will help you understand what's going on under the cover (not to mention that learning functional programming is said to make you a better programmer anyway).
I wrote an overview article on this topic that you may find useful:
Functional Programming using C# and F#
Aside from functional programming, it really helps to understand all C# 3.0 features, most importantly lambda expressions, extension methods and also expression trees. Technically speaking, LINQ is just a clever combination of these three features, so once you understnad them, you can fully appreciate and benefit from LINQ.

101 LINQ samples really helped me to understand LINQ better http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx?ppud=4

There is a series of videos (8, I believe) Scott Stanfield called "LINQ TO SQL." They are very helpful to understand the concept and how to use LINQ.
Link Here
Or, try google: LINQ TO SQL + Scott Stanfield
I particularly like to use, but little use. is always good care ... but it is very useful. Watch the videos and draws its conclusions.
Enjoy!

Related

LINQ to SQL “Active Record” and “Unit Of Work” Pattern

Can you please list references for articles that demonstrates the following two, using LINQ to SQL?
“Active Record” Pattern
“Unit Of Work” Pattern
When I search for them, I am getting most of the examples clubbed with Asp.Net MVC. I don’t need MVC. I am looking for a code implementation (in LINQ to SQL) which demonstrates these two patterns.
I think you'll struggle to find references to such an idea.
To a certain extent, the LINQ to SQL is already implementing these patterns behind the scenes. Therefore, I'm not sure there's much benefit to be gained from layering the additional behavior on top.
Do you have any more detail of what you're trying to achieve that might facilitate a more useful answer?

Deferred Execution (IQueryable) to database level without using ORM?

Is there any easy reasonable/feasible way to efficiently pipeline results from the database?
I can't use any ORM tools. I believe LINQ-SQL does this with it's queryable interfaces which is what I wish to do here.
Based on your answer to the comments above, I think that what youre looking for is the LAZY LOAD PATTERN.
In few words, Martin F. describes this pattern as "An object that doesn't contain all of the data you need but knows how to get it."
So follow these links to learn more:
WIKIPEDIA,
MARTIN FOWLER

SQL to LINQ converter required, any idea?

We have LINQPad which is great for testing Linq expressions targeting database.
Can any one recommend "free ware" tool, that could help us convert
"Sql Queries" to LINQ expressions
and why you recommend it?
NOTE: Please keep in mind, we use c#.
As far as tool, Randy Minder's suggestion of Linqer is the only direct tool I've seen.
That being said, the Visual Basic team did a very good series showing how to go from SQL to LINQ. This walks through what it takes to learn how to do the conversion yourself. Converting from VB's LINQ to C# LINQ is very easy (most of the statements are nearly identical in LINQ), so it's a good way to learn how to do this, even for C# developers.
Even if you're using a tool, I'd recommend trying to understand the concepts - tools don't always produce the best code. Knowing how to do this yourself is very valuable.
The only tool I am aware of that will do this is Linqer. It is not free, but can be purchased for under $50 I believe. It has worked very well for me.

How do I move from Java to C#?

I know Java well. Which caveats and resources will help me cross to the other side (C#) as painlessly as possible.
Biggest tip: go with the .NET naming conventions from the word go. That way you'll constantly be reminded about which language you're in. (Sounds silly, but it really is helpful.) Embrace the idioms of the language as far as possible.
There are various books specifically for folks in your situation - search for "C# for Java" in Amazon and you'll get plenty of hits. It's worth reading carefully to make sure you don't assume that things will work the same in C# as in Java. (For instance, in C# instance variable initializers are executed before the base class constructor body; in Java they happen after. Subtle things like this can take a while to learn, and are easy to miss if you're skimming.)
If you're going to be using C# 3, I'd get a book which definitely covers that - everything in C# 3 will be new to you. Gratuitous plug: my own book (C# in Depth) covers C# 2 and 3, but assumes you already know C# 1. (In other words, it won't be enough on its own, but you may want it as a "second" book.)
See this great article on C# from a Java Developer's Perspective. It has several insights on the things that can be done in both sides to avoid minimum overhead. Having example in both the language you know and the language you want to learn eases the learning curve quite a bit.
Install Visual Studio 2008 and Resharper with IntelliJ IDEA key bindings. This gives you things like prompting you to include namespaces if you start using them.
Start a new project and start writing Java code, when you run into something that doesn't work properly or it's unable to find the class you're trying to use Google "PrintLn in c#".
Write tests or code snippets for sanity checks, like you may want to check if == works for strings (it does)
realize that c# alias Data Types (int is an alias for System.Int32, string for System.String)
look at other peoples code I recommend JP Boodhoos Google code
Take a job in C#, there's lots of jobs requiring both Java and C# especially in support.
Know your libraries, most Java libraries have been ported and most of the time the name is either like (Hibernate => NHibernate) or (Xstream => Xstream.Net). Not every library has an obvious name so just start looking into random ones you hear about here. ie (Rhino.Mocks,HTMLAgilityPack,MBUnit,Rhino.Commons,Castle Project)
Go to usergroup meetings look for a DNUG (Dot Net User Group) they'll be helpful and you can get some good advice.
There's a cheat-sheet from Microsoft for Java developers using C# :)
I know that a good answer has already been accepted. However, I'd like to make an addition...
I find that learning a new language typically involves learning subtle syntactic differences....especially when dealing with the difference between languages in the C/C++/Java/C# family.
In addition to a nice thick reference book I recommend getting a pocket reference like C# 3 Pocket Reference from O'Reilly. It won't help you with the design patterns etc...but will provide a very quick reference about the specific differences of the language you are using.
Here's a quick blurb about this book from that site:
C# 3.0 Pocket Reference includes plenty of illustrations and code examples to explain:
Features new to C# 3.0, such as lambda expressions, anonymous types, automatic properties, and more
All aspects of C# syntax, predefined types, expressions, and operators
Creating classes, structs, delegates and events, enums, generics and constraints, exception handling, and iterators
The subtleties of boxing, operating overloading, delegate covariance, extension method resolution, interface reimplementation, nullable types, and operating lifting
LINQ, starting with the principles of sequences, deferred execution and standard query operators, and finishing with a complete reference to query syntax-including multiple generators, joining, grouping, and query continuations
Consuming, writing, and reflecting on custom attributes
I used this book (well the original) to help me go from being a Java to a C# developer. While I was learning, I kept it by my desk at all times and it really helped.
I made the transition pretty easily by using C# at work, but one of the most important things to do is familiarize yourself with the .NET API and some of the powerful techniques available in C#.
After I learned the .net library I relied on it a lot more than I used to, so learning about the things it can do for you is very helpful. After that, if you work with db code at all, learn LINQ, and also techniques lambas, anonymous types and delegates are also a useful to pick up.
The language syntax is vary similar, so I should only read a small reference of the C# syntax. Like a simple book (for experienced programmers) or maybe wikipedia (http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp) will tell enough.
The biggest difference is the library: Asp.Net websites are totally different from java servlets.
Don't read much, just start programming!
Here's a link that has syntax comparison between Java and C# (even though it's almost identical, there are a few differences).
http://www.harding.edu/fmccown/java1_5_csharp_comparison.html
Use Sharpen to convert your Java programs to C# and see the differences.

Why is C# a functional programmming language?

It has been said that C# can be regarded as a functional programming language, even though it is widely recognized as a OO programming language.
So, what feature set makes C# a functional programming language?
I can only think of:
delegates (even without anonymous methods and lambda expressions)
closures
Anything else?
C# has borrowed a lot of features from ML and Haskell for example:
C# 2.0 brought us parametric polymorphism (or "generics"). I've heard that Dom Syme, one of the creators of F#, was largely responsible for implementing generics in the .NET BCL.
C# 2.0 also allows programmers to pass and returns functions as values for higher-order functions, and has limited support for anonymous delegates.
C# 3.0 and 3.5 improved support anonymous functions for true closures.
LINQ can be considered C#'s own flavor of list comprehensions.
Anonymous types look like an approximation of ML records
Type-inference is a given.
I don't know about you, but C# extension methods look an awful lot like Haskell type classes.
There's been a lot of talk about the "dynamic" keyword in C# 4.0. I'm not 100% sure of its implementation details, but I'm fairly sure its going to use structural typing rather than late binding to retain C#'s compile time safety. Structural typing is roughly equivalent to "duck typing for static languages", its a feature that Haskell and ML hackers have been enjoying for years.
This isn't to say that C# is a functional programming language. Its still missing important features such as pattern matching, tail-call optimization, and list and tuple literals. Additionally, idiomatic C# is fundamentally imperative with a heavy dependence on mutable state.
I wouldn't necessarily consider some of those features mentioned above as exclusive to functional programming languages, but its pretty clear that the C# developers have taken a lot of inspiration from functional programming languages in the past few years.
There being no rigourous definition of "OO Language", "Functional Language", "Procedural Language", one can make arguments that any language fits mostly any classification; one can write procedural Java, object oriented C and functional C++. I typically use a classification based around what the main semantic features support, along with common development practice. A good way of looking at this is to examine builtin and popular frameworks, and see what style they use.
Functional languages are mostly defined as those with first class function primitives, with development styles that use these to reduce complexity with idioms like "map". One other common feature is pattern matching, but I don't see this as exclusively functional. "Pure" functional languages also have no side effects, but that's not mandatory (see how fuzzy these concepts are? :).
So, what's C#? Well, it has first class function style primitives, with delegates (and has gained better syntactic support for the style with anonymous delegates and lambdas). Does this make it functional? Perhaps, if one writes in a functional style. Does the Framework use this style? No, not really.
As such, I wouldn't class C# as functional in general discussion - it is, at best, multi-paradigm, with some functional flavour.
Well, delegates and closures allow it to operate in a largely functional way... for example:
var sum = data.Sum(x=>x.SomeProp);
etc
You can write most higher-order functions using lambdas / delegates. The type inference isn't quite the same as pure functional languages such as F#, but C# generic-type-inference is still pretty good (especially in C# 3.0).
This is especially true in .NET 3.5 and C# 3.0, where LINQ takes a highly-functional approach to many of the problems. But you can still use the functional aspects of C# with .NET 2.0 and C# 2.0. It is just easier with C# 3.0 and lambdas ;-p
Actually, C# is a pragmatic programming language. It aims to make it possible to use a number of paradigms, without punishing you hideously if you want to do something different.
Function pointers is another feature that C# has in the functional category.
I don't think C# is very widely regarded as a functional language, however. I do think it's important to point out that you can program in a functional style in many languages that aren't purely functional.
From Functional Programming:
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.
Using that definition, you can program in a functional style in almost any procedural language. Purely functional languages just enforce it.
I mostly agree with the others here who say that C# is better described as multi-paradigm than functional. But I'd add to the examples of functional features in C# LINQ, a first-class and relatively understandable system for writing monads. While purely functional languages don't require the use of monads, the example of Haskell has shown that they can be extremely useful. Yet they're one of the hardest things go grasp for many people new to Haskell. In C#, on the other hand, many people write LINQ queries these days without even realizing that they're writing monads.
you can find a great overview regarding language features in the presentation from Andrew Kennedy (from MS Research) called C# is a functional programming language. My article about functional programming in C# and F# gives an overview from a higher level perspecitve (especially towards the end).
T.
C# has the some functional language features, closures, for example. The .NET libraries aren't written in a functional style, so in practice C# isn't a functional language. Almost everything is accomplished with mutation. The collection types are all mutable.
These are the main points whow makes c# functional
1-Lamba expresions
2-Extension methods
3-Type inferende
4-Object and collection initializators
5-Closures
6-Anonymous types
7-Linq

Categories