LINQ vs. SQL

Melyik a szimpatikusabb?


select bucid, score, countMatchedEcid, ROW_NUMBER()
OVER(ORDER BY score DESC) AS 'rank'
from (
select
b.ucid as bucid,
a.ucid as aucid,
sum(w.weight) as score,
count(*) as countMatchedEcid
from EcidNameValueSummary a,
genecidparts b,
weights w
where a.id=b.id
and a.value=b.value
and a.ucid!=b.ucid
and w.id=a.id
and w.allowed=1
and w.limit>=a.countEcids
and a.ucid=@ucid
group by b.ucid, a.ucid
) as hits

var matchQuery = from t in
    (from a in c.EcidNameValueSummaries
    join b in c.genecidparts
    on a.id equals b.id
    join w in c.Weights
    on a.id equals w.id
    where w.allowed == 1
    && w.limit >= a.countEcids
    && a.value == b.value
    && a.ucid != b.ucid
    && a.ucid == ucid
    select new
    {
        bucid = b.ucid,
        aucid = a.ucid,
        score = w.weight1
    })
group t by t.bucid into g
orderby g.Sum(e => e.score) descending
select new RankEntity
{
    Ucid = g.Key,
    Score = g.Sum(e => e.score) ?? 0,
    CountMatched = g.Count(),
    Rank = 0
};

BindingListCollection<RankEntity> ranks =
new BindingListCollection<RankEntity>();
int i = 1;
//It would be cool to do this with linq, but...
foreach (RankEntity e in matchQuery)
{
    e.Rank = i++;
    ranks.Add(e);
}
return ranks;

És emellett, a ROW_NUMBER-t át tudná nekem valaki fordítani LINQ-ra?

5 Responses to “LINQ vs. SQL”

  1. Laci Says:

    Bár még LINQ preview-val teszteltem, de iszonyatos performance különbségek vannak az SQL javára…

  2. Soczó Zsolt Says:

    Laci, ezt az aspektusát pont nem néztem még a problémának, de megnézem, és leírom. A bejegyzésben tisztán a küllemre helyeztem a hangsúlyt, mennyire egyszerű/érhető a két kód.

  3. Soci blog » Blog Archive » LINQ vs. SQL teljesítmény Says:

    [...] Soci blog Az ember kivételével minden állat tudja, hogy a legfontosabb dolgunk az életben: élvezni azt. « LINQ vs. SQL [...]

  4. delZubu Says:

    azért a fair play miatt az sql-hez odaírhatnád azt a dataaccess / mapping kódot, ami osztályhierarchiát csinál belőle, mert igaziból szerintem a linq for sql pozitívuma pont ennek az egyszerűsödése (meg hogy ugyanaz a lekérdezésszintaxis nem csak adatbázison, hanem tetszőleges provider-en is értelmezhető)

    különben az sql nekem is olvashatóbb. de lehet hogy csak azért, mert sql-lel már tíz+ éve foglalkozom, linq-val meg csak pár hónapja?

  5. Soczó Zsolt Says:

    delZubu: igazad van, az egyik egy nyers kimenet, a másik meg ojjektumok halmaza. LINQ +pont.

Leave a Reply