Get the first occurance in a dictionary LINQ
I have the following code where it keeps looping even after first
occurrence in the code. I want to stop but I cannot apply Any in my case
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;
}
I want to apply ANY in this section where I get the caption; when I find
the first benefit I want it to exit and assign it:
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();
}
No comments:
Post a Comment