1
我有以下代碼,需要將其轉換爲簡單的語句(刪除使用LINQ)。Linq to simple foreach cicles
任何人都可以幫忙嗎?
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k)
{
List<T> list = new List<T>();
foreach (var element in elements)
{
list.Add(element);
}
var enumerable = elements as IList<T> ?? list;
return k == 0 ? new[] { new T[0] } :
enumerable.SelectMany((e, i) =>
enumerable.Skip(i + 1).Combinations(k - 1).Select(c => (new[] { e }).Concat<T>(c)));
}
你面臨的問題是什麼? –
一切正常,但是當我在SQL CLR ASSEMBLY中調用此函數時,它將返回空引用異常。我想刪除使用linq並從我的程序集中刪除System.Core的引用。我認爲在使用SQL CLR ASSEMBLY –
Linq或沒有Linq中使用System.Core的問題是,如果'elements'爲null,那麼'foreach'循環將導致'NullReferenceException'。 –