0
settingsproperty 컬렉션을 가지고 있습니다. 각 루프에 대해를 사용하여 모든 속성을 반복하고 싶지 않습니다. 대신에 컬렉션을 쿼리하고 싶습니다. 어떻게해야합니까? LINQ를 사용하는 방법이 있습니까? 해?settingsproperty 쿼리 컬렉션을 쿼리하는 방법
감사
settingsproperty 컬렉션을 가지고 있습니다. 각 루프에 대해를 사용하여 모든 속성을 반복하고 싶지 않습니다. 대신에 컬렉션을 쿼리하고 싶습니다. 어떻게해야합니까? LINQ를 사용하는 방법이 있습니까? 해?settingsproperty 쿼리 컬렉션을 쿼리하는 방법
감사
SettingsPropertyValueCollection는 IEnumerable을 < T를 구현하지 않습니다>하지만 IEnumerable을 구현 않습니다. LINQ를 사용하여 쿼리하려는 경우 몇 가지 옵션이 있습니다.
당신은 IEnumerable을하고 쿼리를 소요하고 당신을위한 쿼리 수행하는 경우() 확장 메서드를 만들 수 있습니다
public static class IEnumerableExtensions
{
public static IEnumerable<T> Where<T>(this IEnumerable input, Func<T,bool> query)
{
return input.Cast<T>().Where(item => query(item));
}
}
가 가정 :
var settings = new SettingsPropertyValueCollection
{
new SettingsPropertyValue(new SettingsProperty("Email")
{
DefaultValue = "[email protected]",
PropertyType = typeof(string)
}),
new SettingsPropertyValue(new SettingsProperty("City")
{
DefaultValue = "Austin",
PropertyType = typeof(string)
}),
new SettingsPropertyValue(new SettingsProperty("State")
{
DefaultValue = "TX",
PropertyType = typeof(string)
})
};
사용은 다음과 같습니다
var matches = settings.Where<SettingsPropertyValue>(x => x.Name == "City")
을
또는 LINQ 캐스트 < T> 연산자를 사용하여 se ttings : 당신은 단지 하나 개의 가능한 일치 다음 FirstOrDefault를 (사용 예상되는 경우
var matches = settings.Cast<SettingsPropertyValue>()
.Where(x => x.Name == "City");
) 대신()
var match = settings.Cast<SettingsPropertyValue>()
.FirstOrDefault(x => x.Name == "City");