1

我有以下代码,即使在代码中第一次出现后它也会一直循环。Any我想停止,但我无法申请

    public List<FieldConfiguration> GetListOfProvisionsForBenefits(Dictionary<int, string> benefits)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("SELECT ProvisionID ,BenefitID,ProvisionName, BPROV_Flags FROM BenefitProvisions WHERE");
        int intSQLvar = 0;
        string strSeperator = string.Empty;
        foreach (KeyValuePair<int, string> benefit in benefits)
        {

            sb.AppendFormat(" {0} BenefitID=@benerfit{1}", strSeperator, intSQLvar);
            intSQLvar++;
            strSeperator = "OR";
        }

        SqlConnection con = new SqlConnection(m_strDBConnectionString);
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = sb.ToString();
        intSQLvar = 0;
        foreach (KeyValuePair<int, string> benefit in benefits)
        {
            cmd.Parameters.Add(string.Format("@benerfit{0}", intSQLvar), SqlDbType.Int, 32).Value = benefit.Key.ToString();
            intSQLvar++;
        }

        DataSet ds = new DataSet();
        try
        {
            con.Open();
            sqlDataAdapter.SelectCommand = cmd;
            sqlDataAdapter.Fill(ds);
        }
        finally
        {
            if (con != null)
            {
                con.Close();
                con = null;
            }
        }
        List<ProvisionDetails> lstProvisions = new List<ProvisionDetails>();

        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
                lstProvisions = (from r in ds.Tables[0].AsEnumerable()
                                select new ProvisionDetails()
                        {
                            ID = r.Field<int>("ProvisionID"),
                            Name = r.Field<string>("ProvisionName"),
                            BenefitID = r.Field<int>("ProvisionID"),
                            OptionValue =  r.Field<int>("ProvisionID") 
                        }).ToList();

            if (benefits.Count == 1)
            {
                return (from p in lstProvisions
                        select new FieldConfiguration()
                        {
                            Name = p.Name,
                            ProvisionFieldID = p.ID.ToString(),
                            FieldType = Configuration.SyncapayPlus.FieldType.Provision,
                            Caption = (from b in benefits 
                                           where(b.Key == p.BenefitID)
                                       select string.Format("{0}_{1}", b.Value, p.Name)).ToString() // to do change caption  benefitname_provisionName
                        }).ToList();
            }
            else
            {
                return (from p in lstProvisions.GroupBy(x => x.Name)
                .Where(y => y.Count() > 1)
               .SelectMany(z => z)
                        select new FieldConfiguration()
                        {
                            Name = p.Name,
                            ProvisionFieldID = p.ID.ToString(),
                            FieldType = Configuration.SyncapayPlus.FieldType.Provision,
                            SourceOption = p.OptionValue.ToString(),
                            Caption =
                            (from b in benefits
                             where (b.Key == p.BenefitID)
                             select string.Format("{0}_{1}", b.Value, p.Name)).ToString() // to do change caption  benefitname_provisionName
                        }).ToList();
            }


        }
        return null;

    }

我想在获得标题的本节中应用任何内容;当我找到第一个好处时,我希望它退出并分配它:

Caption =
     (from b in benefits
     where (b.Key == p.BenefitID)
     select string.Format("{0}_{1}", b.Value, p.Name)).ToString() // to do change caption  benefitname_provisionName
                           }).ToList();
            } 
4

4 回答 4

5

如果你知道总会有至少一个这样的好处——或者你不介意使用占位符来代替缺失值——你可以使用这样的东西:

Caption = string.Format("{0}_{1}",
                        benefits.Where(b => b.Key == p.BenefitID)
                                .Select(b => b.Value)
                                .FirstOrDefault(), // Or First
                        p.Name);

我建议您不要过多地混合和匹配您的查询表达式和点表示法 - 目前这很令人困惑。

于 2013-08-15T17:13:05.453 回答
2

您可以在此处使用FirstorDefault。(在我发表评论后将其作为答案发布)

Caption = string.Format("{0}_{1}",
                        benefits.Where(b => b.Key == p.BenefitID)
                                .Select(b => b.Value)
                                .FirstOrDefault(), // Or First
                        p.Name);

它将返回序列的第一个元素,如果序列不包含任何元素,则返回默认值

于 2013-08-15T17:15:41.953 回答
0

我同意您在这里做的太多的评论,这使可读性变得非常困难,因此将使调试和未来的可维护性变得非常困难。

但是,要回答您的问题,您可能需要尝试以下方法:

(from b in benefits
 where (b.Key == p.BenefitID)
 select string.Format("{0}_{1}", b.Value, p.Name)).First()

或者

 (from b in benefits
  where (b.Key == p.BenefitID)
  select string.Format("{0}_{1}", b.Value, p.Name)).FirstOrDefault()

我相信 .ToString() 将是多余的。

于 2013-08-15T17:14:45.360 回答
0

首先,修正缩进,不要在不需要时在查询语法和传统语法之间切换。这将更容易发现和理解问题。

return (from p in lstProvisions
        group x by x.Name into y
        where y.Count() > 1
        from z in y
        select new FieldConfiguration()
          {
            Name = p.Name,
            ProvisionFieldID = p.ID.ToString(),
            FieldType = Configuration.SyncapayPlus.FieldType.Provision,
            SourceOption = p.OptionValue.ToString(),
            Caption = (from b in benefits
                       where b.Key == p.BenefitID
                       select string.Format("{0}_{1}", b.Value, p.Name)
                      ).ToString()
          }
       ).ToList()

在询问有关代码的问题时,您可能还想考虑不使用变量名称,如 x,y,z。

所以,现在,我可以看到您的问题出在哪里:您在查询中使用 ToString,但您可能想要第一个 Caption 代替:

return (from p in lstProvisions
        group x by x.Name into y
        where y.Count() > 1
        from z in y
        select new FieldConfiguration()
          {
            Name = p.Name,
            ProvisionFieldID = p.ID.ToString(),
            FieldType = Configuration.SyncapayPlus.FieldType.Provision,
            SourceOption = p.OptionValue.ToString(),
            Caption = (from b in benefits
                       where b.Key == p.BenefitID
                       select string.Format("{0}_{1}", b.Value, p.Name)
                      ).First() // perhaps FirstOrDefault() or FirstOrDefault() ?? ""
          }
       ).ToList()

或者,如果您想要所有好处的字符串表示,您可以再次使用 string.Join 而不是 First。

于 2013-08-15T17:16:29.093 回答