0

我有这样的原始查询:

string upit = "select f.idFilm as idFIlm, f.naziv as nazivFilm, z.naziv as nazivZanr, f.idZanr as idZanr, f.godina as godina, f.slika, f.klip <br>
from video_klub_sema.film as f <br>
inner join video_klub_sema.zanr as z on z.idZanr=f.idZanr <br>
inner join video_klub_sema.kopija as k on f.idFilm=k.idFilm where ";

if (checkBox1.Checked)
    upit += "k.nije_tu=0 ";
else
    upit += " k.nije_tu in (0,1)";

if (comboBoxGodina.SelectedIndex == 0)
    upit += " and f.godina in (select distinct godina from video_klub_sema.film)";
else
    upit += " and f.godina=" + comboBoxGodina.SelectedItem.ToString();

if (comboBoxZanr.SelectedIndex == 0)
    upit += " and f.idZanr in (select idZanr from video_klub_sema.zanr)";
else
    upit += " and f.idZanr= (select idZanr from video_klub_sema.zanr where naziv = '" + comboBoxZanr.SelectedItem.ToString() + "')";

if (textNaziv.Text != "")
    upit += " and f.naziv like '%" + textNaziv.Text + "%'";

upit += " GROUP BY f.naziv, f.idFilm, z.naziv, f.idZanr, f.godina, f.slika, f.klip";

可以将此查询转换为实体框架 linq 查询吗?

4

1 回答 1

0

对的,这是可能的。查看这些 Linq 示例:

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

IN 运算符可以翻译成 Any 扩展方法。

LIKE 可以翻译成 Contains 扩展方法。

像实体框架中的运算符?

不是最好的解决方案,但我希望这将是一个很好的起点:

 var query = from f in filmTable
                     join z in zanrTable on f.idZanr equals z.idZanr
                     join k in kopijaTable on f.idFilm equals k.idFilm
                     select new
                                {
                                    idFIlm=f.idFilm ,
                                    nazivFilm=f.naziv,
                                    nazivZanr=z.naziv,
                                    idZanr=f.idZanr,
                                    godina=f.godina,
                                    f.slika, 
                                    f.klip,
                                    k.nije_tu
                                };

        if (checkBox1.Checked)
            query =query.Where(k=>k.nije_tu==0);
        else
            query = query.Where(k => k.nije_tu == 0|| k.nije_tu==1);

        var godinaSubQuery = filmTable.Select(f => f.godina).Distinct();
        if (comboBoxGodina.SelectedIndex == 0)
            query = query.Where(f => godinaSubQuery.Contains(f.godina));
        else
            query= query.Where(f=>f.godina== comboBoxGodina.SelectedItem.ToString());

        if (comboBoxZanr.SelectedIndex == 0)
            query = query.Where(f => zanrTable.Select(p => p.idZanr).Contains(f.idZanr));
        else
           query =query.Where(f=>f.idZanr==zanrTable.FirstOrDefault(z=>z.naziv== comboBoxZanr.SelectedItem.ToString().idZanr));

        if (textNaziv.Text != "")
            query =query.Where(f=>f.nazivFilm.Contains(textNaziv.Text));
于 2013-09-08T12:14:12.480 回答