Zināšanu bāze

Darbs ar kolekcijām izmantojot LINQ

Sveicināti,

Manā dvēselē iemitinājās pārmetums, kurš adresēt man personīgi. Esmu pavisam piemirsis par savu zināšanu iemūžināšanu. Ir steidzami reabilitējās. Pēc vairāku mēnešu analītiskā darba beidzot esmu ticis pie kodēšanas, jāatzīst, kodēt man patīk vairāk!

Nesen pārskatīju savu rakstu sarakstu, parādās viela jaunu rakstu tapšanai. Šoreiz paturpināšu tēmu saistībā ar kolekcijām. Saistītie raksti, http://www.dotnet.lv/blogs/am/archive/2007/08/09/ienumerable-lt-t-gt.aspxhttp://www.dotnet.lv/blogs/am/archive/2008/05/22/enumerable-klase-system-linq-v-rdu-telp.aspx

Kā cilvēkam, kas uztur un pilnveido finansu nozarei paredzētas programmas, bieži jādarbojas ar sarakstiem. Un jāatzīst, ka šeit galvenā profesionalitātes pazīme slēpjas spējā rakstīt maksimāli vienkāršu un saprotamu kodu. Apskatīsim reālu piemēru. Izveidots saraks ar viena tipa objektiem.

var list = new[] { new Invoice { Amount = 10M }, new Invoice { Amount = -15M }, new Invoice { Amount = 25M } };

Klase izskatās šādi:

public class Invoice
{
    public decimal Amount { get; set; }
}

Ir nepieciešamība atlasīt tikai objektus ar negatīvām vērtībām. Problēmas var atrisināts vairākos veidos. Iespējams, optimālākais izmantojot .NET Framework 2.0. Viens no īsākiem realizācijas piemēriem:

var result = new List<Invoice>(list).FindAll(i => i.Amount < 0M);

Laimīgi ir tiek cilvēki, kuriem ir iespēja darboties ar .NET Framework 3.5, pateicoties LINQ kods vel vairāk vienkāršojās . Pievienojot norādi uz vārdu telpu

using System.Linq;

Uzrakstot šādu te kodu iegūstam mums vēlamo gadījumu:

var result = list.Where(i => i.Amount < 0M);

Vel vien ļoti izplatīts gadījums, nepieciešams atlasīt pirmo vai pēdējo elementu kolekcijā

Invoice firstItem = null, lastItem = null;
if (list.Length > 0)
{
    firstItem = list[0];
    lastItem = list[list.Length - 1];
}

// tālāk kods, kas veic darbības ar atlasītiem elementiem

Protams var izmantot paņēmienu ar List<T>.First()FirstOrDefault() un List<T>.Last()LastOrDefault(). Bet arī šeit var tiks pielietots LINQ

using System.Linq;
Invoice firstItem = list.FirstOrDefault(), lastItem = list.LastOrDefault();

Metodēm FirstOrDefault un LastOrDefault ir iespēja padot arī filtrācijas nosacījumus.

Jau aktīvi izmantoju šīs konstrukcijas projektu realizācija, blakus parādību nav novērotas:) Tikai stipri samazinās kļūdu iespējamības, kods kļūst maksimāli caurspīdīgs:) Cerams noderēs!

  


Posted Feb 20 2009, 11:50 AM by andrejs.mamontovs

Comments

ivars.arins wrote re: Darbs ar kolekcijām izmantojot LINQ
on 02-21-2009 8:43

FirstOrDefault() ar lambda izteiksmes filtru ir ļoti noderīgs. Varēji jau cilvēkiem izstāstīt, ar ko atšķiras First() un FirstOrDefault() paplašinājuma metodes :)

andrejs.mamontovs wrote re: Darbs ar kolekcijām izmantojot LINQ
on 02-21-2009 16:53

Neliela korekcija rakstā. Lielākā atšķirība starp List<T>.First() un List<T>.FirstOrDefault()  nav. Gadījumā, ja meklēšanas rezultātā netiks atrasti ieraksti, First metode izmetīs kļūdas ziņojumu.

Un vel viens moments, var arī neizmantot Lamda izteiksmes, bet gan izsaukt metodi, kurai kā arguments tiek padots tips Invoice. Filtrācijas nosacījumu pārbaude kalpo kā atgriežamā vērtība  (True vai False).

valdis.iljuconoks wrote re: Darbs ar kolekcijām izmantojot LINQ
on 02-22-2009 21:07

neatceros vai šajā vārud telpā bija ``map'' funkcionalitāte?

let list = [1..5];;

let res = Seq.map (fun arg -> arg * 2) list

> res;;

val it : seq<int> = seq [2; 4; 6; 8; ...]

or nebija? :)

buy google +1 wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-23-2012 20:20

kuUryA I really enjoy the blog article.Thanks Again. Really Cool.

Social Bookmarking Service wrote re: Darbs ar kolekcijām izmantojot LINQ
on 06-20-2012 16:11

RjJ2pi A round of applause for your post.Really looking forward to read more. Great.

Cheap Seo Service wrote re: Darbs ar kolekcijām izmantojot LINQ
on 07-01-2012 3:56

NccQ6u wow, awesome article post. Really Cool.

Web 2.0 Microblog wrote re: Darbs ar kolekcijām izmantojot LINQ
on 07-15-2012 0:05

Fpl5TQ I think this is a real great post.

Article submission wrote re: Darbs ar kolekcijām izmantojot LINQ
on 08-07-2012 18:14

dlkIIk I really enjoy the article.

bookmarking submission wrote re: Darbs ar kolekcijām izmantojot LINQ
on 09-10-2012 3:38

iI1wlQ Hey, thanks for the blog post.Thanks Again. Cool.

cheap seo services wrote re: Darbs ar kolekcijām izmantojot LINQ
on 09-24-2012 10:33

wVeWvJ I appreciate you sharing this post.Really thank you! Will read on...

cheap bookmarking service wrote re: Darbs ar kolekcijām izmantojot LINQ
on 09-25-2012 1:10

iTMbJN I loved your post.Really looking forward to read more. Really Great.

bookmarking submission wrote re: Darbs ar kolekcijām izmantojot LINQ
on 10-08-2012 7:39

XONAbn Im thankful for the blog.Really thank you! Really Cool.

crork wrote re: Darbs ar kolekcijām izmantojot LINQ
on 11-08-2012 15:28

pLJQeJ I appreciate you sharing this blog post. Awesome.

nike free run 2 wrote re: Darbs ar kolekcijām izmantojot LINQ
on 11-20-2012 4:08

If you decide you would definitely maintain the answer by means of an opponent, see getting this done by no means someone.

[url=www.nikeschuhedamenherren.com/nike-free-run-2-laufschuhe-f%C3%BCr-m%C3%A4nner-blau-wei%C3%9F-p-1197.html]nike free run 2[/url]

<a href="www.nikeschuhedamenherren.com/nike-free-run-2-laufschuhe-f%C3%BCr-m%C3%A4nner-blau-wei%C3%9F-p-1197.html" title="nike free run 2">nike free run 2</a>

Social Bookmarking Service wrote re: Darbs ar kolekcijām izmantojot LINQ
on 12-07-2012 23:16

tJnzeJ Thank you for your post.Really looking forward to read more. Fantastic.

generic viagra wrote re: Darbs ar kolekcijām izmantojot LINQ
on 12-24-2012 9:36

5Po9D6 Thanks for the blog post.Thanks Again. Much obliged.

destockchine wrote re: Darbs ar kolekcijām izmantojot LINQ
on 01-09-2013 0:37

On no account scowl, no matter if you were unhappy, because do not no who might be reducing crazy about an individual's be happy.

destockchine www.destockchinefr.fr/.../nike-shox-nz-2-pas-cher

destockchine wrote re: Darbs ar kolekcijām izmantojot LINQ
on 01-10-2013 8:03

A fabulous uncle very likely are not a fellow worker, but yet a fellow worker have invariably been the uncle.

destockchine www.destockchinefr.fr/.../serie-a-pas-cher

archeage gold wrote re: Darbs ar kolekcijām izmantojot LINQ
on 02-27-2013 13:48

I keep listening to the news bulletin lecture about receiving free online grant applications so I have been looking around for the finest site to get one. Could you advise me please, where could i get some? archeage gold www.cheap-archeage-gold.com

casquette pas cher wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-04-2013 7:19

Laughter could be a cologne you should not dump within many more with out ending up with a number sheds within your family. casquette pas cher http://www.c88.fr/

code reduction la redoute wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-05-2013 22:25

True love is almost certainly imperfect located at having your baby, it matures sturdier as they age with the price of suitably provided. code reduction la redoute http://www.k88.fr/

destockchine wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-07-2013 12:19

Due to the fact any person doesn‘h thank you your unique interest in would like them with,doesn‘h indicate that they don‘h thank you along with they have personally. destockchine www.b77.fr/sac-c-19.html

casquette wati b wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-14-2013 2:40

The particular worst case scenario tactic to forget about an individual needs to be relaxing good alongside folks recognizing you can easily‘longer have them. casquette wati b http://www.a77.fr/

casquette obey wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-15-2013 19:16

To everyone you will be one person, however to man or woman you will be the entire world. casquette obey http://www.a44.fr/

casquette los angeles wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-23-2013 8:52

An authentic good friend are you who seem to overlooks your entire suprises and thus tolerates your entire successes. casquette los angeles http://e99.fr/

social bookmarking service wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-23-2013 14:27

RFIK2T Major thanks for the blog article.Much thanks again. Want more.

taille casquette wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-23-2013 22:09

Wear‘big t consider so faithfully, the number one facts occur for those who really be prepared the crooks to. taille casquette http://d77.fr/

casquette supreme wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-23-2013 23:28

Maybe Immortal wishes for me to quite a few incorrect people today previously seminar the correct one, with the intention that whenever we completely satisfy the woman, we intend to have learned to wind up being pleased. casquette supreme http://e11.fr%2

casquette swagg wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-26-2013 3:58

Even if you are going to you may even answer from the an enemy, indicate to understand it to fail to somebody. casquette swagg http://e55.fr/

ny casquette wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-26-2013 19:43

To the world could very well be yourself, but nonetheless , to a single people could very well be the globe. ny casquette http://d88.fr/

e22.fr wrote re: Darbs ar kolekcijām izmantojot LINQ
on 03-30-2013 20:36

Solidarity may possibly be the golden thread the fact neckties the very paper hearts with the industry. e22.fr http://e22.fr/

Social bookmarks wrote re: Darbs ar kolekcijām izmantojot LINQ
on 04-04-2013 1:01

APVJiQ I value the blog post. Fantastic.

comedy shirts wrote re: Darbs ar kolekcijām izmantojot LINQ
on 04-06-2013 4:14

This is one awesome blog.Really looking forward to read more. Cool.

veste redskins wrote re: Darbs ar kolekcijām izmantojot LINQ
on 04-06-2013 21:56

Don‘big t waste content the effort within the wow/lovely lady,people who isn‘big t prepared to waste content ones own precious time you. veste redskins http://www.niketnfrancais.com/

buy social bookmarks wrote re: Darbs ar kolekcijām izmantojot LINQ
on 04-08-2013 9:36

RX7el5 I really like and appreciate your blog post.Really thank you!

social bookmarking service wrote re: Darbs ar kolekcijām izmantojot LINQ
on 04-14-2013 7:15

Y9sQXn Enjoyed every bit of your blog article.Really thank you! Really Great.

basketnike2013.com wrote re: Darbs ar kolekcijām izmantojot LINQ
on 04-15-2013 17:04

Bliss serves as a perfume it's hard to dump upon people without the need for purchasing a hardly any lowers upon for yourself. basketnike2013.com http://www.basketnike2013.com/

Add a Comment

(obligāts)  
(brīvizvēles)
(obligāts)  
Remember Me?