I want to convert this this piece of code to a LINQ query as LINQ is much quicker than a foreach. I dont know how to go about doing it, can do basically simple LINQ queries.
What i am trying to do is get a specific field in the dictionary.
Dictionary<string, object> fieldLayout = null;
foreach (Dictionary<string, object> dic in pageLayout)
{
if (dic[ "FIELD" ].ToString() == "123")
{
fieldLayout = dic;
break;
}
}
fieldLayout = pageLayout.FirstOrDefault(dic => dic["FIELD"].ToString() == "213");
LINQ几乎不比foreach
快,但这超出了这里的范围。
var fieldLayout = pageLayout.FirstOrDefault(pl => pl["FIELD"].ToString() == "123");
Firstly, LINQ is not faster than a foreach loop.
Here is the implementation:
Dictionary<string, object> fieldLayout = pageLayout.Where(x => x["FIELD"].ToString() == "123").FirstOrDefault();
Or even better:
Dictionary<string, object> fieldLayout = pageLayout.FirstOrDefault(x => x["FIELD"].ToString() == "123");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.