标签: generic-list

C# - 确定List <T>是否脏了?

我正在序列化作为我的数据实体的类的列表.我有一个包含List的DataProvider.

我总是直接修改集合中的项目.

确定列表中的任何项目是否已更改的最佳方法是什么?我正在使用Compact Framework.

我当前唯一的想法是在加载列表时创建List的哈希值(如果可能的话).然后,当我进行保存时,我重新获取列表的哈希值,看看它们是否是不同的值.如果它们不同,我保存然后更新存储的Hash以便稍后进行比较,如果它们是相同的则我不保存.

有任何想法吗?

c# hash compact-framework generic-list c#-2.0

4
推荐指数
1
解决办法
7421
查看次数

检查List <T>日期的最快方法

我有一个机器已经处理的日期列表,但它没有包含机器停机的日期.我需要创建一个工作日但没有工作的列表.我不确定这样做的最好方法.我已经开始通过递增范围的所有日期并通过每次迭代整个列表来检查日期是否在列表中.我正在寻找一种更有效的方法来查找日期.

class machineday
{
 datetime WorkingDay;
}

class machinedaycollection : List<machineday>
{
public List<TimeCatEvent> GetAllByCat(string cat)
{
  _CategoryCode = cat;


  List<machineday> li = this.FindAll(delegate(machinedaydummy) { return true; });
  li.Sort(sortDate);
  return li;
}

int sortDate(machinedayevent1, machinedayevent2)
{
  int returnValue = -1;
  if (event2.date < event1.date)
  {
    returnValue = 0;
  }
  else if (event2.date == event1.date)
  {
    //descending
    returnValue = event1.date.CompareTo(event2.date);
  }
  return returnValue;
}
}
Run Code Online (Sandbox Code Playgroud)

c# generics generic-list .net-2.0

4
推荐指数
1
解决办法
1277
查看次数

如何使用Linq对通用列表进行排序?

如何在linq中对myScriptCellsCount.MyCellsCharactersCount(list int type)进行排序

    public class MyExcelSheetsCells
    {
        public List<int> MyCellsCharactersCount { get; set; }

        public MyExcelSheetsCells()
        {
            MyCellsCharactersCount = new List<int>();
        }

    }
Run Code Online (Sandbox Code Playgroud)
   void ArrangedDataList(DataTable dTable)
        {
            DAL.MyExcelSheets myexcelSheet = new DAL.MyExcelSheets();
            myScriptCellsCount = new TestExceltoSql.DAL.MyExcelSheetsCells();

            foreach (DataColumn col in dTable.Columns)
                myexcelSheet.MyColumnNames.Add(col.ColumnName.ToString());
            foreach(DataColumn dc in dTable.Columns)
            foreach (DataRow  dr in dTable.Rows)
                myScriptCellsCount.MyCellsCharactersCount.Add(dr[dc].ToString().Length);
          //How can i sort desc
            //myScriptCellsCount.MyCellsCharactersCount = from list in myScriptCellsCount.MyCellsCharactersCount
            //                                            orderby list.CompareTo( descending
            //                                            select list;
            CreatSqlTable(myexcelSheet.MyColumnNames, dTable.TableName, myScriptCellsCount.MyCellsCharactersCount[0].ToString());
            myscript.WriteScript(myscript.SqlScripts);
        }
Run Code Online (Sandbox Code Playgroud)

c# linq sorting generic-list .net-3.5

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

C#:从其基类的实例(泛型列表)返回一个继承的类

这可能是我完全倒退的事情,但我想更多地了解我做错了什么......

我已经声明一个类只是一个通用列表的直接继承(完成简化命名),如下所示:

public class FooList : List<Foo> {}
Run Code Online (Sandbox Code Playgroud)

现在在另一个完全独立于这个类的方法中,我试图返回这个类的实例,但是我想根据一个标准过滤类,所以我使用了一个lambda表达式:

var list = new FooList(); // imagine this fills it with different items
var filtered = list.FindAll(c => c.Something == "filter criteria");
Run Code Online (Sandbox Code Playgroud)

现在根据FindAll方法,这应该返回一个List [Foo].但是,我想将此对象作为FooList返回,而不是List [Foo].我是否必须创建一个新的FooList实例并从List [Foo]中复制这些项目?

如果是这样,为什么?为什么我不能直接将List转换为FooList,因为它们是同一个对象?

如果可以这样做,我该怎么办?

非常感谢!

c# inheritance generic-list

4
推荐指数
2
解决办法
822
查看次数

使用指定属性按字母顺序对通用对象列表进行排序

我正在编写地址簿程序。我将每个人的详细信息存储在List<Person>. 我需要能够按姓氏(如果有关系则使用名字)或邮政编码对这个列表进行排序。

到目前为止,我有这个:

public class Person
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string PostCode { get; set; }
    // etc..
}

public class AddressBook
{
    public List<Person> People { get; set; }

    // asc: ascending or descending
    // column: the property to use when sorting
    //         (in my case either LastName or Postcode)
    public void Sort(bool asc, string column)
    {
        // What should I put here?
    }

    // …
Run Code Online (Sandbox Code Playgroud)

c# sorting generic-list

4
推荐指数
3
解决办法
1万
查看次数

使用反射的通用列表

我有一个场景,我得到了Class name as a string我只想创建List该类的泛型.我尝试了以下内容

            Type _type = Type.GetType(className);

            List<_type> list = new List<_type>();
Run Code Online (Sandbox Code Playgroud)

但我认为error.我不能把那个_type作为列表参数.如果有人知道请帮助我

c# reflection generic-list

4
推荐指数
1
解决办法
3368
查看次数

在C#中构建唯一对象列表的最佳方法

我想知道在C#中构建一个唯一的对象列表是否会更快地遵循一种或另一种模式:

选项1

  • 将所有项添加到通用列表中
  • 调用list.Distinct函数就可以了

选项2

  • 迭代每个项目
  • 检查项目是否已存在于列表中,如果不存在则添加

c# performance generic-list

4
推荐指数
1
解决办法
7142
查看次数

Java泛型 - 使用实现接口的元素列表

在Java中我有一个interface I,一个class C和一个class D,都实现I:

interface I
{
    //some method signatures
}

class C implements I
{
    //implement methods from I
}

class D implements I
{
    //implement methods from I
}
Run Code Online (Sandbox Code Playgroud)

现在我创建一个List持有元素class C,以及第二个List持有元素D:

List<C> c = new LinkedList<C>();
List<D> d = new LinkedList<D>();
Run Code Online (Sandbox Code Playgroud)

我有一个方法,我想应用于所有持有元素实现的列表interface I:

public void modifyList(List<I> l)
{
    //call some method defined in I
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试调用该函数时,编译器会抛出一个错误:

public void testModification()
{
    modifyList(c);
    modifyList(d);
}

==> …
Run Code Online (Sandbox Code Playgroud)

java generics interface generic-list

4
推荐指数
1
解决办法
1055
查看次数

List <T> vs array perfomance

我尝试设置List <int>值

List< int > a;
//...
a[i] = X;
Run Code Online (Sandbox Code Playgroud)

ilspy显示设置编译为:

callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::set_Item(int32, !0)
Run Code Online (Sandbox Code Playgroud)

但是这段代码

int[] b;
//...
b[i] = Y;
Run Code Online (Sandbox Code Playgroud)

编译成

stelem.i4
Run Code Online (Sandbox Code Playgroud)

用我的基准测试,它的速度提高了7倍.

据我所知,虚拟通话比stelem更贵.是否可以使用List <T>与数组perfomace

更新

码:

   static void Main(string[] args)
    {
        int N = int.Parse(args[0]);
        int M = int.Parse(args[1]);

        var sw = new Stopwatch();
        sw.Start();
        int[] a = new int[N];
        for (int k = 0; k < M; ++k)
        {
            for (int i = 0; i < N; ++i)
            {
                a[i] = i * 2; …
Run Code Online (Sandbox Code Playgroud)

.net c# performance bytecode generic-list

4
推荐指数
1
解决办法
170
查看次数

如何在Delphi调试期间查看通用tList

我使用的是Delphi 10.3.1 COMMUNITY版本,在调试项目时无法查看通用的tList。

我知道最新版本的Delphi不支持允许查看通用tList的旧式调试功能。因此,我在以下代码中使用了tList.List来评估tList。

tList<tRecord>.List我可以看看它,但不能做它tList<Integer>.List

在此处输入图片说明

type
  tRecord = record
    Field: Integer;
  end;

procedure TForm1.FormCreate(Sender: TObject);
var
  _Record: tRecord;
  _List1: TList<tRecord>;
  _List2: TList<Integer>;
  i: Integer;
begin
  _List1 := TList<tRecord>.Create;
  _List2 := TList<Integer>.Create;

  for i := 0 to 4 do
  begin
    _Record.Field := i;

    _List1.Add(_Record);
    _List2.Add(i);
  end;

  Caption := IntToStr(_List1.List[0].Field) + IntToStr(_List2.List[0]);

  _List1.Free;
  _List2.Free;
end;
Run Code Online (Sandbox Code Playgroud)

tList<Integer>在调试过程中如何查看?

delphi generics debugging generic-list tlist

4
推荐指数
1
解决办法
150
查看次数