标签: ienumerable

哪个集合没有实现 IEnumerable

是否有任何类型的集合System.Collections不继承自IEnumerableIEnumerable<T>

我需要一个例子。

.net c# collections ienumerable

0
推荐指数
1
解决办法
1856
查看次数

从列表中收集索引

c#中是否有linq函数,可以从特定范围的索引中收集IEnumebles?

一个例子是

        var objectArray = new string[] { "Bill", "Bob", "Joe", "Phil", "Tom", "Paul" };
        var indexArray = new int[] { 1, 3, 5 };

        var list = objectArray.Where(SOME_FUNCTION_TO_GET_INDEXES ??).ToList();

        //output would be list:
        //Bob
        //Phil
        //Paul
Run Code Online (Sandbox Code Playgroud)

c# linq ienumerable list

0
推荐指数
2
解决办法
81
查看次数

如何将List转换为IEnumerable?

我正在解决一个需要编写一个函数的问题,该函数只返回一个对象List中的整数(我编写了函数但它不起作用).我已经想了很长时间如何将List转换为IEnumerable,我正在寻找如何解决问题,但我还没有找到合适的解决方案.拜托,任何人都可以帮我解决这个问题吗?

public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems)
{
    List<object> result = new List<object>();
    for (int i = 0; i < listOfItems.Count - 1; i++)
    {
        if (listOfItems[i] is string)//the input is only integers and strings
        {
            listOfItems.RemoveAt(i);
        }
    }
    return listOfItems;//this doesn't work
}
Run Code Online (Sandbox Code Playgroud)

c# ienumerable list

0
推荐指数
1
解决办法
2813
查看次数

从集合中删除项目的最佳做法

我有一个这样的课

Public Class Car
    Public Property Brand As String
    Public Property Model As String
    Public Property Horsepower As Integer
End Class
Run Code Online (Sandbox Code Playgroud)

并且像这样从这个类中创建了一个对象的集合

Dim myCarCollection As List(Of Car) = New List(Of Car) From {
New Car() With {.Brand = "VW", .Model = "Golf", .Horsepower = "100"},
New Car() With {.Brand = "Mercedes", .Model = "C220", .Horsepower = "110"},
New Car() With {.Brand = "Porsche", .Model = "911", .Horsepower = "341"}}
Run Code Online (Sandbox Code Playgroud)

现在,例如,我想删除所有品牌不是大众汽车且马力小于300的汽车.哪种"最好"的方式呢?我看到这个系列有类似的东西myCarCollection.Where,有人可以解释一下如何做到这一点吗?

编辑:我知道如何使用for/ foreach但我正在考虑采用更智能的方法.

.net vb.net collections ienumerable list

0
推荐指数
1
解决办法
57
查看次数

如何将viewmodel修改为Expression <Func <T,bool >>?

捎带一个非常相似的问题 ......

我需要从ViewModel生成一个表达式作为搜索谓词传递.我需要能够根据用户提供的内容包含/排除查询参数.例:IQueryable.Where

public class StoresFilter
{
    public int[] Ids { get; set; }

    [StringLength(150)]
    public string Name { get; set; }

    [StringLength(5)]
    public string Abbreviation { get; set; }

    [Display(Name = "Show all")]
    public bool ShowAll { get; set; } = true;

    public Expression<Func<Store, bool>> ToExpression()
    {
        List<Expression<Func<Store, bool>>> expressions = new List<Expression<Func<Store, bool>>>();

        if (Ids != null && Ids.Length > 0)
        {
            expressions.Add(x => Ids.Contains(x.Id));
        }
        if (Name.HasValue())
        {
            expressions.Add(x => x.Name.Contains(Name)); …
Run Code Online (Sandbox Code Playgroud)

c# ienumerable iqueryable func expression-trees

0
推荐指数
1
解决办法
120
查看次数

0
推荐指数
1
解决办法
58
查看次数

计算bool属性为真的实例数 - C#

我有一个IEnumerable的某个具有bool属性的对象.我想计算在紧凑(在代码行方面)和可读方式中此属性设置为true的对象数量.

为了演示它,我创建了一个带有布尔属性'InnerProperty'的类'Obj'.静态函数'CountInner'实现上面定义的逻辑.我怎样才能更紧凑地实现它?

public class Obj
{
    private bool InnerProperty { get; set; } = false;

    public static int CountInner(IEnumerable<Obj> list)
    {
        var count = 0;
        foreach (var l in list)
        {
            if (l.InnerProperty)
            {
                count++;
            }
        }
        return count;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# linq ienumerable

0
推荐指数
1
解决办法
625
查看次数

将IList <IList <int >>转换为单个展平的哈希集

我有一个包含以下数据的列表:

megalist = { new List {1,2}, new List {1,2}, new List{3}};
Run Code Online (Sandbox Code Playgroud)

现在,我想将此列表IList转换为单个扁平化hashset,应该如下所示:

set = { 1,2,3 } 
Run Code Online (Sandbox Code Playgroud)

我尝试过, megalist.Cast<ISet<int>>().SelectMany(sublist => sublist);但返回错误:

无法将类型为'System.Collections.Generic.List'1 [System.Int32]'的对象强制转换为'System.Collections.Generic.ISet'1 [System.Int32]'.

这种方法有问题吗?非常感谢.

c# ienumerable casting list

0
推荐指数
1
解决办法
243
查看次数

为什么LINQ不确定?

我随机排序了一个IEnumerable.我继续打印出相同的元素,并获得不同的结果.

string[] collection = {"Zero", "One", "Two", "Three", "Four"};
var random = new Random();
var enumerableCollection = collection.OrderBy(e => random.NextDouble());

Console.WriteLine(enumerableCollection.ElementAt(0));
Console.WriteLine(enumerableCollection.ElementAt(0));
Console.WriteLine(enumerableCollection.ElementAt(0));
Console.WriteLine(enumerableCollection.ElementAt(0));
Console.WriteLine(enumerableCollection.ElementAt(0));
Run Code Online (Sandbox Code Playgroud)

每次写入都会给出不同的随机元素.为什么订单没有保留?

在.NET Fiddle上看到它

c# linq ienumerable non-deterministic deferred-execution

0
推荐指数
1
解决办法
168
查看次数

只能在IEnumerable上调用Cast和OfType

在我正在使用的库中,遇到调用IEnumerable之外的任何LINQ方法的问题。我有一个如下的类层次结构(命名有点奇怪,因为它被内部代码所混淆了)

Item : GeneralObject

ItemCollection : GenericCollection<ItemCollection, Item>

GenericCollection<TCollection, TItem> : GeneralObjectCollection, IEnumerable<TItem>
    where TCollection : GenericCollection<TCollection, TItem>
    where TItem : GeneralObject

GeneralObjectCollection : ICollection, IEnumerable<GeneralObject>
Run Code Online (Sandbox Code Playgroud)

可以看出,IEnumerable在ItemCollection的类层次结构中有两次,因此有两种GetEnumerator方法,一种提供a GeneralObject,一种提供a Item

当我查看VS2019中通过元数据提供的类定义时,实现的每个类IEnumerable<TItem>也显示为Implemented IEnumerable

With this setup, I am unable to call most LINQ methods like Select, Where, etc, on any ItemCollection instances, and can only do those on IEnumerable. It looks like it should clearly support methods on IEnumerable<T> as well, but …

c# linq ienumerable

0
推荐指数
1
解决办法
254
查看次数