我正在使用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进行数据检索.执行以下查询时:(注意lstProjects是List<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 …
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)
你对此有解释吗?
在通过Reflector 查看System.Linq.Enumerable时,我注意到用于Select和Where扩展方法的默认迭代器- 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) 我有classT,实施interfaceIBar.
我有一个list类型的变量List<T>.
提高我对语言理解的两个问题:
为什么这不起作用?
var foo = (ICollection <IBar>)list; // fails!
如何解决它(如果可能的话)?
它发生在EF中你必须使用ICollection作为你的属性,
eg. public virtual ICollection<Question> Questions { get; set; }
Run Code Online (Sandbox Code Playgroud)
但我不想在该列表中使用LINQ,这在ICollection中是不可能的.
我应该把它投入IQueryable吗?怎么样?或者实现这一目标的标准方法是什么?
我有一个可以实现各种集合和数据类型的接口,它可以很好地处理某些集合,但字典给我带来了问题,我猜是因为字典有点不同并且有键值对?
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) 我已经在其他语言中找到了一些这个问题的例子,例如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) 我需要将一个字符串列表转换为一个对象列表,但问题是我接收这个列表作为一个对象,因为它是一个参数,我不知道它是什么类型.
这是接收参数的函数:
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)
我该怎么做才能将它作为一个对象列表,然后我可以将它传递给另一个函数?(如果有办法)
我有一节课:
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) 我有一个自定义集合如下:(我不会显示该类的所有代码)
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)
为什么我不能在test1和test2中使用代码
icollection ×10
c# ×9
list ×3
casting ×2
collections ×2
linq ×2
arrays ×1
datetime ×1
dictionary ×1
generics ×1
ienumerable ×1
ilist ×1
interface ×1
performance ×1
toarray ×1