Linq to entities无法将类型'string'隐式转换为'bool'linq查询

时间:2012-06-09 16:58:19

标签: entity-framework linq-to-entities

我正在研究asp.net应用程序,我已经创建了一个查询。我想更改它,以便如果选中复选框,则无法输入字段的空记录。我的查询看起来像这样:

var query = from d in context.data_vault.Where(d => states.Contains(d.STATE) 
    && counties.Contains(d.COUNTY)
    && cities.Contains(d.CITY)
    && zipCodes.Contains(d.ZIP)
    && areaCodes.Contains(d.AREA_CODE)
    && (d.MKTVAL > Convert.ToInt32(txtMarketValueFrom) && d.MKTVAL < Convert.ToInt32(txtMarketValueTo.Text))
    && (d.LTV > Convert.ToByte(txtLTVFrom.Text ) && d.LTV < Convert.ToByte(txtLTVTo.Text))
    && d.FHA_LIMIT_FLAG == chkFHA.Checked
    && (d.MTGAMT > Convert.ToInt32(txtMortgageFrom.Text) && d.MTGAMT < Convert.ToInt32(txtMortgageTo.Text))
    && (d.MTGDATE > Convert.ToDateTime(txtMortgageDateFrom.Text) && d.MTGDATE < Convert.ToDateTime(txtMortgageDateTo.Text))
    && d.LOAN_TYPE == drpMortgageLoanType.Text
    //&& (chkLenderName.Checked )  d.LENDERNAME != null  
    && lenderNames.Contains(d.LENDERNAME)
    //&& rdoexcludeSecondMortgage.Checked ? d.HE == "" : rdoexcludeSecondMortgage.Checked
    //&& d.HEAMT = txtSecondMortgageFrom.Text
    && d.HE_LOAN_TYPE == drpSecondMortgageLoanType.SelectedItem.Text
    && d.HELENDERNAME == txtSecondMortgageLenderName.Text
    && (d.AGE > Convert.ToByte(txtAgeFrom.Text) && d.AGE < Convert.ToByte(txtAgeTo.Text)
    //rdoNoPhone.Checked ? d.phones
    ))
    group d by new
    {
        d.STATE
    } into g
    select new
    {
        g.Key.STATE,
        Count = (Int32?)g.Sum(p => p.SCORE),
        Phone = (Int64?)g.Count(p => p.PHONE != null)
    };

GridView1.DataSource = query;
GridView1.DataBind();

如何更改它,以便在选中rdoNoPhone.Checked单选按钮时,不包括带有空电话的记录

1 个答案:

答案 0 :(得分:0)

尝试改变你的符号:

 var query = from d in context.data_vault.Where(delegate(ata_vault d){

  return states.Contains(d.STATE) 
    && counties.Contains(d.COUNTY)
    && cities.Contains(d.CITY)
    && zipCodes.Contains(d.ZIP)
    && areaCodes.Contains(d.AREA_CODE)
    && (d.MKTVAL > Convert.ToInt32(txtMarketValueFrom) && d.MKTVAL < Convert.ToInt32(txtMarketValueTo.Text))
    && (d.LTV > Convert.ToByte(txtLTVFrom.Text ) && d.LTV < Convert.ToByte(txtLTVTo.Text))
    && d.FHA_LIMIT_FLAG == chkFHA.Checked
    && (d.MTGAMT > Convert.ToInt32(txtMortgageFrom.Text) && d.MTGAMT < Convert.ToInt32(txtMortgageTo.Text))
    && (d.MTGDATE > Convert.ToDateTime(txtMortgageDateFrom.Text) && d.MTGDATE < Convert.ToDateTime(txtMortgageDateTo.Text))
    && d.LOAN_TYPE == drpMortgageLoanType.Text
    //&& (chkLenderName.Checked )  d.LENDERNAME != null  
    && lenderNames.Contains(d.LENDERNAME)
    //&& rdoexcludeSecondMortgage.Checked ? d.HE == "" : rdoexcludeSecondMortgage.Checked
    //&& d.HEAMT = txtSecondMortgageFrom.Text
    && d.HE_LOAN_TYPE == drpSecondMortgageLoanType.SelectedItem.Text
    && d.HELENDERNAME == txtSecondMortgageLenderName.Text
    && (d.AGE > Convert.ToByte(txtAgeFrom.Text) && d.AGE < Convert.ToByte(txtAgeTo.Text)
    && rdoNoPhone.Checked ? d.PHONE!= null : true
    })
    group d by new
    {
        d.STATE
    } into g
    select new
    {
        g.Key.STATE,
        Count = (Int32?)g.Sum(p => p.SCORE),
        Phone = (Int64?)g.Count(p => p.PHONE != null)
    };