Linq to SQL:如何执行[列] = LIST中的元素?

Linq to SQL : How to do WHERE [Column] = Element in LIST ?
2020-10-25
  •  译文(汉语)
  •  原文(英语)

首先,我见过很多类似我的文章,但没有找到答案.

因此,我尝试为linq到SQL请求查找等效于"IN"子句的内容,然后找到了一些东西:

    IList<int> listClientId = GetAllClientIdByRegionLPS(statutRegion);
    if (!statutRegion.Equals(""))
        q = q.Where(p => listClientId.Contains((int)p.ClientId));

但这使我出错:"'boolean contains(int32)'不支持sql转换"

我想做的事:从...中选择…[条件]并且(id == 1 OR id == 2 OR id == 3 OR ...)

让我们看看我的代码:

public IList<Appel> GetAllCourants(string Filter, int? StatutFacturation, int? statutDemande, int? statutPro, string statutRegion)
    {
        var q = from p in DbContext.Appels
                where (p.Client.Nom.Contains(Filter) || p.AppelId.ToString().Contains(Filter) || (p.Materiel.Modele1.Nom + " #" + p.Materiel.NumeroSerie).Contains(Filter) || p.ContactClient.Nom.Contains(Filter) || p.ContactClient.Prenom.Contains(Filter) || p.Dossier.Contains(Filter) || p.Client.CodePostal.Contains(Filter) || p.Client.Ville.Contains(Filter) || p.NumDevis.Contains(Filter))
                select p;
        q = q.Where(p => p.StatutAppelId == 1 || p.StatutAppelId == 9 || p.StatutAppelId == 10 || p.StatutAppelId == 13 || p.StatutAppelId == 14 || p.StatutAppelId == 15 || p.StatutAppelId == 16 || p.StatutAppelId == 17 || p.StatutAppelId == 18);

        if (StatutFacturation.HasValue)
            q = q.Where(p => p.Facturable == StatutFacturation.Value);

        if (statutDemande.HasValue)
            q = q.Where(p => p.StatutDemandePiece == statutDemande.Value);

        if (statutPro.HasValue)
            q = q.Where(p => p.Depot == statutPro.Value);
        //This makes me troubles
        IList<int> listClientId = GetAllClientIdByRegionLPS(statutRegion);
        if (!statutRegion.Equals(""))
            q = q.Where(p => listClientId.Contains((int)p.ClientId)); /*Try to do p.ClientId = listClientId*/

        var ret = q.Select(p => Deserialize<Generated.Appel>(p)).ToList();
        return ret;
    }

我该如何解决?

在此先多谢!

解决过程1

尝试改变

IList<int> listClientId = GetAllClientIdByRegionLPS(statutRegion);

List<int> listClientId = GetAllClientIdByRegionLPS(statutRegion);
速聊1:
非常感谢,就是这样!

First of all, i've seen many posts like mine but I didn't find the answer.

So, I try to find the equivalent of the "IN" clause for an linq to SQL request and i find something :

    IList<int> listClientId = GetAllClientIdByRegionLPS(statutRegion);
    if (!statutRegion.Equals(""))
        q = q.Where(p => listClientId.Contains((int)p.ClientId));

But that makes me an error : "'boolean contains(int32)' has no supported translation to sql"

What i want to do : SELECT ... FROM ... WHERE [CONDITIONS] AND (id == 1 OR id ==2 OR id==3 OR ...)

Let's see my code :

public IList<Appel> GetAllCourants(string Filter, int? StatutFacturation, int? statutDemande, int? statutPro, string statutRegion)
    {
        var q = from p in DbContext.Appels
                where (p.Client.Nom.Contains(Filter) || p.AppelId.ToString().Contains(Filter) || (p.Materiel.Modele1.Nom + " #" + p.Materiel.NumeroSerie).Contains(Filter) || p.ContactClient.Nom.Contains(Filter) || p.ContactClient.Prenom.Contains(Filter) || p.Dossier.Contains(Filter) || p.Client.CodePostal.Contains(Filter) || p.Client.Ville.Contains(Filter) || p.NumDevis.Contains(Filter))
                select p;
        q = q.Where(p => p.StatutAppelId == 1 || p.StatutAppelId == 9 || p.StatutAppelId == 10 || p.StatutAppelId == 13 || p.StatutAppelId == 14 || p.StatutAppelId == 15 || p.StatutAppelId == 16 || p.StatutAppelId == 17 || p.StatutAppelId == 18);

        if (StatutFacturation.HasValue)
            q = q.Where(p => p.Facturable == StatutFacturation.Value);

        if (statutDemande.HasValue)
            q = q.Where(p => p.StatutDemandePiece == statutDemande.Value);

        if (statutPro.HasValue)
            q = q.Where(p => p.Depot == statutPro.Value);
        //This makes me troubles
        IList<int> listClientId = GetAllClientIdByRegionLPS(statutRegion);
        if (!statutRegion.Equals(""))
            q = q.Where(p => listClientId.Contains((int)p.ClientId)); /*Try to do p.ClientId = listClientId*/

        var ret = q.Select(p => Deserialize<Generated.Appel>(p)).ToList();
        return ret;
    }

How can I fixe it ?

Thanks a lot in advance !

Solutions1

Try changing

IList<int> listClientId = GetAllClientIdByRegionLPS(statutRegion);

to

List<int> listClientId = GetAllClientIdByRegionLPS(statutRegion);
Talk1:
Thanks a lot, it was that !!
转载于:https://stackoverflow.com/questions/30399961/linq-to-sql-how-to-do-where-column-element-in-list

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入