Linq to SQL 学习笔记

1.Where语句中的&&连接必须每个条件加括号,否则不报错,但生成的SQL不正确

var data = from d in TradeContext.CustomerKind
                        where ((textBoxCustomerKindName.Text.Trim().Length <= 0 ? true : d.CustomerKindName.Contains(textBoxCustomerKindName.Text.Trim()))
                        && (textBoxMemo.Text.Trim().Length <= 0 ? true : d.Memo.Contains(textBoxMemo.Text.Trim()))
                        && (checkBoxAllowUsed.Checked ? d.AllowUsed == radioButtonYes.Checked : true))
                        select new
                        {
                            客户类型编码 = d.CustomerKindCode,
                            客户类型名称 = d.CustomerKindName,
                            备注信息 = d.Memo,
                            允许使用 = d.AllowUsed
                        };

 

2.WHERE语名中不能进行类型转换,要放到LINQ外转换好

int nCode = Convert.ToInt32(comboBoxArea.SelectedValue.ToString());
            var data = from d in TradeContext.Country
                       where ((comboBoxArea.SelectedIndex == 0 ? true : d.AreaCode == nCode)
                       && (textBoxCountryName.Text.Trim().Length <= 0 ? true : d.CountryName.Contains(textBoxCountryName.Text.Trim()))
                       && (textBoxEnglishName.Text.Trim().Length <= 0 ? true : d.EnglishName.Contains(textBoxEnglishName.Text.Trim()))
                       && (checkBoxAllowUsed.Checked ? d.AllowUsed == radioButtonYes.Checked : true))
                       select new
                       {
                           国家编码 = d.CountryCode,
                           所属地区 = d.Area.AreaName,
                           国家名称 = d.CountryName,
                           英文名称 = d.EnglishName,
                           国家域名 = d.NationalDomain,
                           允许使用 = d.AllowUsed
                       };

你可能感兴趣的:(LINQ)