标签: icollection

Linq从ICollection <T>转换为List <T>

我正在使用Code First Entity Framework.

我使用以下简单类:

public class Users
{
    public ICollection<Projects> Projects{get;set;}
}

public class Projects
{
    public ICollection<Users> Users{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

我正在使用linq进行数据检索.执行以下查询时:(注意lstProjectsList<Project>)

var lstUsers = (from users in lstProjects
                where users.ProjectId == pId
                select users.Users).ToList();
Run Code Online (Sandbox Code Playgroud)

我有一个List<Users>对象,并希望用项目填充此列表.喜欢,

var lstUsersToDisplay = new List<Users>();
lstUsersToDisplay = (List<Users>)lstUsers; //This can't be cast.
Run Code Online (Sandbox Code Playgroud)

什么是转换的方式ICollection<T>List<T>

其次,我已经List<Users>并希望将其转换为ICollection<Users>如何实现这一目标?

编辑: 场景,更清楚的是所有Projects都被加载lstProjects,我们需要选择Users映射到特定项目的.这些用户也包含在Projects集合中.如果我分解了lstProjects,那么每个Project都有它的Users collection样子:

lstProjects …

c# linq linq-to-entities entity-framework icollection

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

我无法访问数组的Count属性,但通过强制转换为ICollection!

        int[] arr = new int[5];
        Console.WriteLine(arr.Count.ToString());//Compiler Error
        Console.WriteLine(((ICollection)arr).Count.ToString());//works print 5
        Console.WriteLine(arr.Length.ToString());//print 5
Run Code Online (Sandbox Code Playgroud)

你对此有解释吗?

c# arrays icollection

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

为什么WhereSelectArrayIterator没有实现ICollection?

在通过Reflector 查看System.Linq.Enumerable时,我注意到用于SelectWhere扩展方法的默认迭代器- WhereSelectArrayIterator - 没有实现ICollection接口.如果我正确读取代码,这会导致一些其他扩展方法,如Count()ToList()执行较慢:

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
    // code above snipped
    if (source is List<TSource>)
    {
        return new WhereSelectListIterator<TSource, TResult>((List<TSource>) source, null, selector);
    }
    // code below snipped
}

private class WhereSelectListIterator<TSource, TResult> : Enumerable.Iterator<TResult>
{
    // Fields
    private List<TSource> source; // class has access to List source so can implement ICollection
    // code below snipped
}


public class …
Run Code Online (Sandbox Code Playgroud)

c# performance list icollection toarray

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

将List <T>(其中T:IBar)转换为ICollection <IBar>失败

我有classT,实施interfaceIBar.

我有一个list类型的变量List<T>.

提高我对语言理解的两个问题:

  • 为什么这不起作用?

    var foo = (ICollection <IBar>)list; // fails!

  • 如何解决它(如果可能的话)?

c# casting interface list icollection

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

在ICollection(EF)中使用LINQ


它发生在EF中你必须使用ICollection作为你的属性,

eg. public virtual ICollection<Question> Questions { get; set; }
Run Code Online (Sandbox Code Playgroud)

但我不想在该列表中使用LINQ,这在ICollection中是不可能的.
我应该把它投入IQueryable吗?怎么样?或者实现这一目标的标准方法是什么?

linq entity-framework icollection

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

以 ICollection 形式返回字典

我有一个可以实现各种集合和数据类型的接口,它可以很好地处理某些集合,但字典给我带来了问题,我猜是因为字典有点不同并且有键值对?

public interface IStructure
{
    void InsertRun<T> (T item);
    ICollection RetrieveSortedListRun<T>();
    T RetrieveItemRun<T>(T item);
}

class DictionaryRun : IStructure
{
    IDictionary<int, object> dictionary;

    public DictionaryRun()
    {
        dictionary = new Dictionary<int, object>();
    }

    public void InsertRun<T>(T item)
    {
        dictionary.Add(dictionary.Count + 1, item);
    }

    public ICollection RetrieveSortedListRun<T>()
    {
        return dictionary;
    }

    public T RetrieveItemRun<T>(T item)
    {
        return item;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# generics collections dictionary icollection

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

为什么我不能在C#中的另一个类的DateTime对象上调用DateTime方法

我已经在其他语言中找到了一些这个问题的例子,例如ruby或php,它们似乎表明我需要某种包含来支持这个但我无法弄清楚它.

我有:

private void setLoansView(Member _member)
{

    foreach (Loan loan in _member.Loans)
        {
            this.dt.Rows.Add(_member.Name, // dt is a datatable 
                   loan.BookOnLoan.CopyOf.Title, 
                   loan.TimeOfLoan.ToShortDateString(), 
                   loan.DueDate.ToShortDateString(), 
                   loan.TimeReturned.ToShortDateString());
        }
Run Code Online (Sandbox Code Playgroud)

贷款看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace Library.Entities
{
    public class Loan
    {
        [Key]
        public int LoanId { get; set; }
        [Required]
        public DateTime? TimeOfLoan { get; set; }
        public DateTime? DueDate { get; set; }
        public DateTime? TimeReturned { get; set; }
        [Required]
        public Copy BookOnLoan { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# datetime icollection

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

C#将对象转换为List <object>

我需要将一个字符串列表转换为一个对象列表,但问题是我接收这个列表作为一个对象,因为它是一个参数,我不知道它是什么类型.

这是接收参数的函数:

public static bool IsNotEmpty(object obj)
{
    if (obj is ICollection)
    {
        IList<object> collection = (IList<object>)obj; // The cast throws error here
        return IsNotEmpty(collection);
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)

这就是这个人使用的:

public static bool IsNotEmpty<T>(IList<T> aList)
{
    return aList != null && aList.IsNotEmpty();
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能将它作为一个对象列表,然后我可以将它传递给另一个函数?(如果有办法)

c# ienumerable list icollection

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

添加值时,IList <int>会抛出Null Reference Exception

我有一节课:

public class ClientModelData
{
    public int clientID { get; set; }
    public IList<int> LocationIDs { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我打电话的时候:

ClientModelData obj = new ClientModelData();
obj.LocationIDs.Add(1);
Run Code Online (Sandbox Code Playgroud)

它引发了一个异常:

`((System.Collections.Generic.ICollection<int>)(client.LocationID))' is null`
Run Code Online (Sandbox Code Playgroud)

c# collections ilist icollection

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

使用as铸造自定义集合失败

我有一个自定义集合如下:(我不会显示该类的所有代码)

public class MyCollection : IList<MyOBject>, ICollection<MyOBject>, IEnumerable<MyOBject>, IEnumerable, IDisposable
{
    //Constructor
    public MyCollection (MyOBject[] shellArray);
    // blah blah
}
Run Code Online (Sandbox Code Playgroud)

我只想要集合中SomeValue = false的条目,我想弄清楚为什么我不能使用as运算符,如下所示:

MyCollection SortCollection(MyCollection collection)
{
    MyCollection test1 = collection.Where(x => (bool)x["SomeValue"].Equals(false)) as MyCollection ; //test1 = null

    var test2 = collection.Where(x => (bool)x["SomeValue"].Equals(false)) as MyCollection ;          //test2 = null

    var test3 = collection.Where(x => (bool)x["SomeValue"].Equals(false)); //test3 is non null and can be used
    return new MyCollection (test3.ToArray());
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能在test1test2中使用代码

c# casting icollection

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