How do I tell the cases when it’s worth to use LINQ?

Many things in LINQ can be accomplished without the library. But for some scenarios, LINQ is most appropriate.

Examples are:

  1. SELECT – https://stackoverflow.com/questions/11883262/wrapping-list-items-inside-div-in-a-repeater
  2. SelectMany, Contains – https://stackoverflow.com/questions/11778979/better-code-pattern-for-checking-existence-of-value
  3. Enumerable.Range – https://stackoverflow.com/questions/11780128/scalable-c-sharp-code-for-creating-array-from-config-file
  4. WHERE https://stackoverflow.com/questions/13171850/trim-string-if-a-string-ends-with-a-specific-word

What factors to take into account when deciding between LINQ and regular .Net language elements?

8

I’ve found LINQ to be handy in literally any scenario where I’m working with collections of objects. The main things you do with collections of objects are:

  • Search them for items matching some characteristic
  • Traverse them to calculate some aggregate value
  • Visit each item in the collection and calculate some value based on it (translation)
  • Rearranging its contents for some purpose (transformation)

Each of these scenarios has a number of LINQ methods that are designed to help with the task:

  • For searching, there’s Where(), First(), Any(), All(), Last()
  • For aggregation, there’s Aggregate(), Average() Sum(), Max(), Min()
  • For translation there’s Select() and SelectMany()
  • For transformation there’s OrderBy(), GroupBy(), Reverse()

And a few other odds and ends that I’m not sure how to categorize in the above for – for example the set operations Union() and Intersect().

In general, all of these functions do the work of hand-coded loops, but have the advantage of improving legibility because they help to replace several lines worth of code with a few LINQ operations that are named after what they do. For example, consider

string[] input;

var output = new string[input.Length];
for(int i = 0; i < input.Length; i++)
{
    output[input.Length - i - 1] = input[i];
}

versus:

var output = input.Reverse().ToArray();

Another advantage is that it’s easy to string them together to get more complex behavior. Say that instead you wanted to get only the unique values in the array, and sort them. Right now I’m feeling too lazy to hand-code a solution to it, but I’m sure whatever it is it would be much longer and harder to read than

var output = input.Distinct().OrderBy(x=>x).ToArray();

And it definitely would take longer, which is why I don’t feel like writing it.

There are a number of different types of technologies which fall under the umbrella term of LINQ:

LINQ-to-Objects

LINQ-to-SQL

LINQ-to-Datasets

LINQ-to-XML

LINQ-to-Entities

And there are more provided by others as well, such as LINQ-to-CSV, LINQ-to-NHibernate, etc.

In every one of these cases, the purpose of LINQ is the same: to query sets of data. So anytime you have some sort of set (0, 1 or more items) of a particular type of data, LINQ is a succinct solution to deal with it. Many questions seem to arise over whether it performs better than a hand-written approach, but that’s not the point. LINQ specifies intent rather than implementation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật