c# - Get the first occurance in a dictionary LINQ -
i have following code keeps looping after first occurrence in code. want stop cannot apply any
in case
public list<fieldconfiguration> getlistofprovisionsforbenefits(dictionary<int, string> benefits) { stringbuilder sb = new stringbuilder(); sb.append("select provisionid ,benefitid,provisionname, bprov_flags 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); } { 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() // 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 (b.key == p.benefitid) select string.format("{0}_{1}", b.value, p.name)).tostring() // change caption benefitname_provisionname }).tolist(); } } return null; }
i want apply in section caption; when find first benefit want exit , assign it:
caption = (from b in benefits (b.key == p.benefitid) select string.format("{0}_{1}", b.value, p.name)).tostring() // change caption benefitname_provisionname }).tolist(); }
if know there'll @ least 1 such benefit - or don't mind using placeholder missing value - can use this:
caption = string.format("{0}_{1}", benefits.where(b => b.key == p.benefitid) .select(b => b.value) .firstordefault(), // or first p.name);
i'd advise not mix , match query expressions , dot notation though - it's quite confusing @ moment.
Comments
Post a Comment