标签: generic-list

C# - List <T> .Remove()始终删除列表中的第一个对象

在Visual Studio 2008中工作(C#)...我使用List集合来存储我的自定义类(Shift)的实例.

我想使用Remove方法从列表中删除某个班次.

但List.Remove()总是删除它找到的第一个项目.

我为我的Shift实现了IComparable接口,我认为这就够了,然后我添加了IEqualityComparer的实现,它仍然没有效果.

以下是我的实施摘录:

地区IComparable会员

    public int CompareTo(object obj)
    {
        Shift s1 = this;
        Shift s2 = (Shift)obj;
        if (s1.start.time != s2.start.time)
            return s1.start.CompareTo(s2.start);
        else
            return s1.end.CompareTo(s2.end);
    }
Run Code Online (Sandbox Code Playgroud)

endregion

region IEqualityComparer成员

    public bool Equals(Shift x, Shift y)
    {

        if ((x.opening) != (y.opening)) return false;
        if ((x.closing) != (y.closing)) return false;
        if (!x.opening) if (x._start != y._start) return false;
        if (!x.closing) if (x._end != y._end) return false;
        if (x.when != y.when) return false;
        if (x.day != y.day) return …
Run Code Online (Sandbox Code Playgroud)

.net c# equality generic-list iequalitycomparer

5
推荐指数
1
解决办法
5916
查看次数

通过DataTable迭代查找List对象中的元素?

当我遍历DataTable对象时,我需要针对通用字符串List中的项检查每个DataRow对象.

我发现了一个使用List的Find方法和一个委托的博客帖子,但是那个例子有一个单独的类(Person),我正在使用字符串对象的实例尝试类似下面的内容:

// My definition of the List object.
List<string> lstAccountNumbers = new List<string>();
...

// I populate the List via its Add method.
...

foreach (DataRow drCurrentRow in dtMyDataTable.Rows) 
{
    if (lstAccounts.Find(delegate(string sAccountNumber) { return sAccountNumber == drCurrentRow["AccountNumber"]; })
    {
        Found_DoSomething();
    }
    else
    {
        NotFound_DoSomethingElse();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,使用这种语法我收到"不能隐式地将类型'字符串'转换为'bool'"作为if块.

有人可以澄清我做错了什么以及如何最好地完成我想要做的事情?

c# asp.net datatable datarow generic-list

5
推荐指数
1
解决办法
2865
查看次数

排序双打的通用列表

我有一个通用的双打列表,在页面上显示如下:

1199.17
1199.17
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1199.17
1349.17
1349.17
1349.17
1349.17
1349.17
1349.17
1311.67
1311.67
1311.67
1311.67
1311.67
1349.17
2174.17
2174.17
2174.17
2174.17
2136.67
2136.67
2136.67
2136.67
2174.17
2361.67
2361.67
2361.67
2361.67
2361.67
2361.67
2361.67
2361.67
2399.17
2849.17
2849.17
2849.17
2849.17
2849.17
2849.17
2849.17
2849.17
3111.67
3111.67
3111.67
3149.17
Run Code Online (Sandbox Code Playgroud)

我试图订购它们,以便最低的双倍是第一个.

我尝试了doublePriceList.Sort()但这不起作用.

我怎样才能做到这一点?

.net c# generic-list

5
推荐指数
2
解决办法
2万
查看次数

使用expressions/lambda设置属性值的通用方法

我试图找到一种通用的方法来为lambda表达式指定的属性赋值,查看下面的示例代码,ConverToEntities方法的签名将如何显示以及如何调用?

static void Main()
{
    List<long> ids = new List<long> {1, 2, 3};

    //Non generic way
    List<Data> dataItems = ids.ConvertToDataItems();

    //Generic attempt!!
    List<Data> differntDataItems =
        ids.ConvertToEntities<Data>( p => p.DataId );
}

public class Data
{
    public long DataId;
    public string Name;
}

public static class ExtensionMethods
{
    public static List<Data> ConvertToDataItems(this List<long> dataIds)
    {
        return dataIds.Select(p => new Data { DataId = p }).ToList();
    }

    public static List<T> ConvertToEntities<TProp>(
        this List<long> entities, Func<TProp> lambdaProperty )
    {
        return entities.Select(p => new …
Run Code Online (Sandbox Code Playgroud)

.net c# linq generic-list

5
推荐指数
1
解决办法
2103
查看次数

从List <T>中查找最大值

我定义了许多对象,每个对象都有一个名为"CreateDate"的属性.

是否可以编写单个通用方法从我指定的对象中选择最高日期?

我试图使用通用方法,但当我尝试指定属性名称时,编译器不喜欢它.

我试图在这些方面取得成就......

private static DateTime GetLastDate<T>(List<T> data)
{
    // Unfortunately, this is not allowed...
    return
        (from d in data
        orderby d.CreateDate
        select d.CreateDate).FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

c# list generic-list c#-4.0

5
推荐指数
1
解决办法
1683
查看次数

c#中的泛型列表类

我目前正在制作我自己的非常基本的通用列表类(以便更好地理解预定义的列表类如何工作).我遇到的唯一问题是我无法像通常那样使用" System.Collections.Generic.List" 来访问数组中的元素.

GenericList<type> list = new GenericList<type>();
list.Add(whatever);
Run Code Online (Sandbox Code Playgroud)

这工作正常,但在尝试访问我希望能够编写的"任何"时:

list[0];
Run Code Online (Sandbox Code Playgroud)

但这显然不起作用,因为我在代码中明显遗漏了一些东西,我需要添加到我完全正常工作的泛型类中是什么?

c# generics generic-list

5
推荐指数
1
解决办法
477
查看次数

创建一个无法在外部更改的List属性

我的VB.NET项目中有一个公共类,它有一个List(Of String)属性.这份名单需要由其他类项目中修改,但由于可能(在未来的一段时间)的项目外露出的课,我希望它是不可修改的,在水平.修改项目中现有属性只能通过调用列表的方法(特别是.Add偶尔.Clear)来完成,而不是通过用新列表批量替换属性值(这就是为什么我将它作为ReadOnly属性).

我想出了一种方法,但我不确定它是什么,你会称之为"优雅".

就是这样:

Friend mlst_ParameterNames As List(Of String) = New List(Of String)

Public ReadOnly Property ParameterNames() As List(Of String)
    Get
        Return New List(Of String)(mlst_ParameterNames)
    End Get
End Property
Run Code Online (Sandbox Code Playgroud)

现在,这只是工作正常和花花公子.项目中mlst_ParameterNames直接访问该字段的任何类都可以根据需要对其进行修改,但是通过公共属性访问它的任何程序都可以将其修改为内容,但由于属性过程始终返回副本,因此无处可去.列表,而不是列表本身.

但是,当然,这带来了开销,这就是为什么我觉得它只是......好吧,在某种程度上看起来"错误",即使它有效.

参数列表永远不会很大.它最多只包含50个项目,但通常少于10个项目,所以我看不出这是一个性能杀手.然而,它当然让我认为有些人可以拥有更加整洁和清洁的想法.

任何人?

vb.net properties class generic-list

5
推荐指数
1
解决办法
3919
查看次数

List的实现是什么?

我读了这段代码:

List<long> userIdList = new List<long>();
Run Code Online (Sandbox Code Playgroud)

但我跳到List(in System.Collections.Generic)的定义(使用VS2012 ),我发现:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
    // Summary:
    //     Initializes a new instance of the System.Collections.Generic.List<T> class
    //     that is empty and has the default initial capacity.
    [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    public List();
    //
    // Summary:
    //     Initializes a new instance of the System.Collections.Generic.List<T> class
    //     that contains elements copied from the specified collection and has sufficient
    //     capacity …
Run Code Online (Sandbox Code Playgroud)

.net c# generics generic-list

5
推荐指数
2
解决办法
1万
查看次数

将值添加到基于模型的List

Backgournd

我目前正在尝试将一个值添加到模型列表中,但它似乎与我在JavaScript中的方式不同.

模型

public class ContentBase
{

    public string Name { get; set; }    
    public string Description { get; set; }
    public string LastModifiedTime { get; set; }
    public string KeyWords { get; set; }
    public string Content { get; set; }
    public string UniqueId { get; set; }
    public string library { get; set; }
    public string ContentSummary { get; set; }        
    public string[] images { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

功能增加价值

 List<ContentBase> returnList = new List<ContentBase>();
 foreach (HtmlNode img …
Run Code Online (Sandbox Code Playgroud)

.net c# list generic-list

5
推荐指数
1
解决办法
67
查看次数

C#反射,获取重载方法

我已经检查了一些关于反射和重载方法的其他帖子,但可以找到任何帮助。我发现的一篇文章是this one,但这并没有多大帮助。

我有以下两种方法:

1 | public void Delete<T>(T obj) where T : class { ... }
2 | public void Delete<T>(ICollection<T> obj) where T : class { ... }
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取方法 N°1。

我尝试了经典GetMethod("Delete")方法,但由于有两个具有此名称的方法,因此Ambiguous抛出了-Exception。我尝试使用附加参数指定方法架构,例如GetMethod("Delete", new [] { typeof(Object) })没有找到任何东西(返回空值)。

我想我不妨循环遍历所有方法并检查参数。

我写了以下方法...

    public static IEnumerable<MethodInfo> GetMethods(this Type type, String name, Type schemaExclude)
    {
        IEnumerable<MethodInfo> m = type.GetRuntimeMethods().Where(x => x.Name.Equals(name));
        return (from r in m let p = r.GetParameters() where !p.Any(o => schemaExclude.IsAssignableFrom(o.ParameterType)) select r).ToList();
    } …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection overloading generic-list

5
推荐指数
1
解决办法
1037
查看次数