gpt4 book ai didi

c# - 获取字典中的第一个匹配项 LINQ

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:54 30 4
gpt4 key购买 nike

我有以下代码,即使在代码中第一次出现后它也会继续循环。我想停止,但我不能在我的情况下应用 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();
}

最佳答案

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

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

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

关于c# - 获取字典中的第一个匹配项 LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18257834/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com