简体   繁体   English

linq to sql query with checkboxes条件组合

[英]linq to sql query with checkboxes where condition combination

if checkbox chkAus checked : 如果复选框chkAus已选中:

if (chkAus.Checked)
                {
                    vAdvanceSearchResults  = (from t in vAdvanceSearchResults
                                    join imp in _bDataContext.Imprints on t.ImprintID equals imp.ID
                                    join cc in _bDataContext.CountryCodes on imp.CountryID equals cc.ID
                                    where cc.Code.Contains("AU")
                                    select t).Distinct();**
                }

if chkAus and chkNz are checked 如果检查了chkAus和chkNz

if (chkNz.Checked && chkAus.Checked)
            {
                vAdvanceSearchResults = (from t in vAdvanceSearchResults
                                join imp in _bDataContext.Imprints on t.ImprintID equals imp.ID
                                join cc in _bDataContext.CountryCodes on imp.CountryID equals cc.ID
                                where cc.Code.Contains("AU") || cc.Code.Contains("NZ")
                                select t).Distinct();
            }

The condition on linq query changes as the checkboxes are checked. linq查询的条件随着选中复选框而改变。

where cc.Code.Contains("AU") || cc.Code.Contains("NZ")

I have nearly 10 checkboxes, and got stuck on how to write that many conditions. 我有将近10个复选框,并且在如何编写这么多条件上陷入困境。 Any help please. 请帮忙。

for example if there is chkUS : then the combination with chkAus,chkNz,chkUS checkboxes the linq query would change. 例如,如果存在chkUS :则linq查询的chkAus,chkNz,chkUS复选框的组合会更改。

where cc.Code.Contains("AU") || cc.Code.Contains("NZ") || cc.Code.Contains("US")

Put all of them in a list and then do a if list.contains(cc.Code) 将它们全部放入列表中,然后执行if list.contains(cc.Code)

var a = new List<string>(){"AU","NZ","US"};
var linq =  (from t in vAdvanceSearchResults
                                join imp in _bDataContext.Imprints on t.ImprintID equals imp.ID
                                join cc in _bDataContext.CountryCodes on imp.CountryID equals cc.ID
                                where a.Contains(cc.Code)
                                select t).Distinct();

Create a list of selected checkboxes first. 首先创建所选复选框的列表。 Like this. 像这样。

var selectedCountries = new List<string>();
if (chkAus.Checked) selectedCountries.Add("AU");
if (chkNz.Checked) selectedCountries.Add("NZ");
if (chkUs.Checked) selectedCountries.Add("US");
//... And so on

And then modify your linq query to check whether this list contains the code or not, I mean reversing the comparison is an answer. 然后修改您的linq查询,以检查此列表是否包含代码,我的意思是反转比较是一个答案。 Make sure you remove if condition for this linq query. 确保删除此linq查询的条件。

vAdvanceSearchResults = (from t in vAdvanceSearchResults 
    join imp in _bDataContext.Imprints on t.ImprintID equals imp.ID 
    join cc in _bDataContext.CountryCodes on imp.CountryID equals cc.ID
    where  selectedCountries.Contains(cc.Code)
    select t).Distinct();

This will fix your problem. 这将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM