Mam taką linjke kodu:
foreach (SPGroup group in user.Groups)
if (group.Name == groupName)
Czy ktoś może mi napisać jak mogę zamiast tego użyć Linq.. Dzięki.
Mam taką linjke kodu:
foreach (SPGroup group in user.Groups)
if (group.Name == groupName)
Czy ktoś może mi napisać jak mogę zamiast tego użyć Linq.. Dzięki.
from @group in user.Groups
where @group.Name == groupName
user.Groups.Where(group => group.Name == groupName)
Skąd masz to 'Where' ?
pyt. napisał(a):
Skąd masz to 'Where' ?
Z System.Linq
.
Chodzi o to że user.Groups. nie może wziąć Where
Info: SPUser user, string groupName
Where może zostać użyte na dowolnym obiekcie implementującym IEnumerable<T>. Jeżeli możesz zrobić na nim foreach to Where również zadziała.
Ogólnie zrobiłem tak:
return user.Groups.Where(g => g.Name == groupName);
ale nie przesło
Co to znaczy nie przeszło? Kompilator nie przepuścił, komputer wybuchł, użytkownik dostał biegunki?
Jaki błąd? Dołączyłeś System.Linq do usings? Jaki typ zwraca ta funkcja? Być może musisz wykonać ".ToArray()" albo zmienić zwracany typ tej funkcji na IEnumerable<T>.
tzn. Error 1 'Microsoft.SharePoint.SPGroupCollection' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'Microsoft.SharePoint.SPGroupCollection' could be found (are you missing a using directive or an assembly reference?)
Natomiast jak zapodam coś takiego: return user.Groups.Cast<SPGroup>().Any(g => g.Name == groupName); to kompiluje się, tylko nie jestem zgodne z tym co zapodałem w pierwszym poście
może .net < 3.5?
czy napisałeś na górze pliku using System.Linq
?
Jeśli tak, czy kompilator widzi taką przestrzeń nazw?
Takie coś działa?:
using System.Linq;
using System;
namespace Tests
{
public class Test
{
public void Foo()
{
string[] arr = { "ala", "zosia", "stefan" };
var girls = arr.Where(x => x[x.Length - 1] == 'a');
foreach (var girl in girls)
{
Console.WriteLine(girl);
}
}
}
}
are you missing a using directive or an assembly reference?
Ah. Nie użyjesz w tym przypadku Linq, bo Microsoft.SharePoint.SPGroupCollection
nie implementuje IEnumerable<T>, tylko starą, niegeneryczną wersję tego interfejsu - IEnumerable.
Zostań przy foreach. Ewentualnie możesz dołożyć yield i zwracać IEnumerable<SPGroup>
.
Ah. Nie użyjesz w tym przypadku Linq, bo Microsoft.SharePoint.SPGroupCollection nie implementuje IEnumerable<T>, tylko starą, niegeneryczną wersję tego interfejsu - IEnumerable.
Da radę, trzeba zrobić .Cast<Type>
ArrayList al = new ArrayList( new[] { "ala", "zosia", "stefan" });
var girls = al.Cast<string>().Where(x => x[x.Length - 1] == 'a');
foreach (var girl in girls)
{
Console.WriteLine(girl);
}
Zarejestruj się i dołącz do największej społeczności programistów w Polsce.
Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.