Użycie Linq

0

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.

0
from @group in user.Groups
where @group.Name == groupName
user.Groups.Where(group => group.Name == groupName)
0

Skąd masz to 'Where' ?

0
pyt. napisał(a):

Skąd masz to 'Where' ?

Z System.Linq.

0

Chodzi o to że user.Groups. nie może wziąć Where

Info: SPUser user, string groupName

0

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.

0

Ogólnie zrobiłem tak:

return user.Groups.Where(g => g.Name == groupName);

ale nie przesło

0

Co to znaczy nie przeszło? Kompilator nie przepuścił, komputer wybuchł, użytkownik dostał biegunki?

0

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>.

0

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

0

może .net < 3.5?

0

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);
            }
        }
    }
}
0

are you missing a using directive or an assembly reference?

0

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>.

0

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);
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1