我想创建一个实现的自定义集合ICollection.
但我不想暴露一些ICollection类似Clear方法的成员.
怎么做到这一点?
因此,很多时候我们有一个接受IEnumerable或ICollection作为参数的函数.如果我们有单个项目,但没有集合来保存它们,我们必须在将它们传递给函数之前创建一个集合,如:
T o1, o2, o3;
Foo(new T[] { o1, o2, o3 });
Run Code Online (Sandbox Code Playgroud)
我总是创建一个数组或列表,就像我在上一个例子中所做的那样.但我想知道,是否有更优雅的方式来创建所需的IEnumerable或ICollection?
如果能做到这一点,那将是非常酷的:
Foo({ o1, o2, o3 });
Run Code Online (Sandbox Code Playgroud)
编译器将创建最抽象的可能集合,以满足IEnumerable或ICollection的需要(取决于函数接受哪一个).
无论如何,如何将o1,o2和o3传递给IEnumerable或ICollection参数?
通过StackOverflow中的旅行,有许多ToList()用于ICollection(或派生自的类ICollection<T>)的扩展方法.
我已经退出了好的'反编译器,看看ToList()扩展方法是如何执行的,并且随时枚举集合.通过源头我遇到了:
[__DynamicallyInvokable]
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
return new List<TSource>(source);
}
Run Code Online (Sandbox Code Playgroud)
现在这非常简单,因此ToList()扩展方法只是List<T>使用源对象创建一个新的.深入挖掘List构造函数会显示此行.
ICollection<T> ts = collection as ICollection<T>;
//.. ommited code
ts.CopyTo(this._items, 0);
Run Code Online (Sandbox Code Playgroud)
就像你想的那样,你会深入研究这种CopyTo方法.这是我被卡住的地方.在完成所有挖掘方法调用之后,我们进入最后的extern方法调用
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[SecurityCritical]
internal static extern void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable);
Run Code Online (Sandbox Code Playgroud)
我的问题是(虽然我似乎无法找到它)ToList()扩展方法(ICollection<T>.CopyTo())在执行时是否遍历Collection(Array)的所有对象?
子问题:我在哪里可以找到上述来源extern void Copy()? …
我需要获取查询结果:
var query = from m in db.SoilSamplingSubJobs where m.order_id == id select m;
Run Code Online (Sandbox Code Playgroud)
并准备作为ICollection,以便我可以有类似的东西
ICollection<SoilSamplingSubJob> subjobs
Run Code Online (Sandbox Code Playgroud)
目前我创建了一个不符合我需求的列表:
query.ToList();
Run Code Online (Sandbox Code Playgroud)
我该怎么办 - 是query.ToIcollection()?
我正在使用Entity Framework CodeFirst,我使用ICollection作为父子关系
public class Person
{
public string UserName { get;set}
public ICollection<Blog> Blogs { get; set;}
}
public class Blog
{
public int id { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
好的,到目前为止一切正常,但我担心的是,每当我想获得一个人的博客时,我就把它当成了
var thePerson = _context.Persons.Where(x => x.UserName = 'xxx').SingleOrDefault();
var theBlogs = thePerson.Blogs.OrderBy(id).Take(5);
Run Code Online (Sandbox Code Playgroud)
现在,我明白,当执行该行时,该人的所有博客都会加载到内存中,然后从内存中进行排序和选择.这对于拥有大量博客的人员的记录来说并不理想.我想将Blog Child设为IQueryable,以便在拉到Memory之前在SQL数据库中完成排序和选择.
我知道我可以在我的上下文中将Blogs声明为IQueryable,以便我可以直接查询为
var theBlogs = _context.Blogs.Where(.....)
Run Code Online (Sandbox Code Playgroud)
但由于设计选择,这对我来说是不可行的,因为序列化问题,我想尽可能避免任何循环引用.所以,我没有在我孩子的父母实体中做任何参考.
我发现,我可以在博客上调用AsQueryable()方法
var theBlogs = thePerson.Blogs.AsQueryable().OrderBy(id).Take(5);
Run Code Online (Sandbox Code Playgroud)
这看起来对我来说太神奇了,看起来好得令人难以置信.所以我的问题.这个AsQueryable是否真的使ICollection成为现实中的IQueryable并在SQL Server中进行所有查询过程(延迟加载)或者它只是一个将博客像以前一样加载到内存中的转换,但是将接口从ICollection更改为IQueryable?
c# iqueryable icollection ef-code-first entity-framework-4.1
我尝试做静态类,添加到icollection但我遇到了一些我似乎无法克服的问题.这就是我如何得到所以我可以在方法中传递ICollection?因为T就是说它无法解决.
然后我想知道有没有办法在icollection上做AddRange?
我在考虑这样的事情,但maby我已经不在乎了吗?
public static ICollection<T> add(this IEnumerable<T> list)
{
ICollection<T> collection = null;
return collection.AddRange(list);
}
Run Code Online (Sandbox Code Playgroud) 我有三个系列:
private ICollection<FPTAssetClassAsset> wrassets;
private ICollection<FPTFundAsset> wrfunds;
private ICollection<FPTManagedStrategyAsset> wrstrats;
Run Code Online (Sandbox Code Playgroud)
如果foreach循环返回0个对象,则集合不会被设置,因此为null.当我将这个icollection(Union)添加到另一个icollection时,它失败了:"Value不能为null"因为icollection为null,而不是Empty.我怎样才能将此集合设置为空?
环:
public void GetWrapperAssets(FPT fpt)
{
foreach (var w in fpt.CouttsPositionSection.Wrappers
.Union(fpt.StandAloneSection.Wrappers)
.Union(fpt.BespokePropositionSection.Wrappers)
.Union(fpt.NonCouttsPositionSection.Wrappers)
)
{
foreach (var a in w.UnderlyingAssets.OfType<FPTManagedStrategyAsset>())
{
wrstrats.Add(a);
}
foreach (var a in w.UnderlyingAssets.OfType<FPTAssetClassAsset>())
{
wrassets.Add(a);
}
foreach (var a in w.UnderlyingAssets.OfType<FPTFundAsset>())
{
wrfunds.Add(a);
}
}
}
Run Code Online (Sandbox Code Playgroud) 根据MSDN文档,System.Array实现ICollection,但System.Array不提供Count属性(当然,您总是可以使用LINQ Count()扩展方法,但是没有具有此名称的属性).怎么会这样?是不是需要数量?
我有以下代码(请注意,这是剥离到相关部分,实际查询要复杂得多):
public IQueryable<Menu> GetMenus(DateTime lastUpdate) {
...
result = GetAll().Where(m => lastUpdate < m.LastModified)
.ForEach(m => m.Descriptions = m.Descriptions
.Where(d => lastUpdate < d.LastModified));
...
enter code here
Run Code Online (Sandbox Code Playgroud)
这是一个更新服务例程中的一个函数,用于获取任何菜单,自上次调用更新服务以来,该菜单本身或其任何描述都已更改.
澄清:该函数需要返回自上次调用以来已更改的每个菜单.此外,它需要返回每个更改的菜单的每个更改的描述.但它必须省略未改变的描述.
举个例子:
Menu menuA = new Menu() {
LastModified = new DateTime(2014, 12, 24),
Descriptions = new List<Description>() {
new Description() { LastModified = new DateTime(2014, 12, 24) },
new Description() { LastModified = new DateTime(2014, 12, 01) }
}
};
Menu menuB = new Menu() {
LastModified = new DateTime(2014, …Run Code Online (Sandbox Code Playgroud) 我的具有一对多映射的域类通常采用以下形式(未经测试的代码):
public Customer Customer
{
// Public methods.
public Order AddOrder(Order order)
{
_orders.Add(order);
}
public Order GetOrder(long id)
{
return _orders.Where(x => x.Id).Single();
}
// etc.
// Private fields.
private ICollection<Order> _orders = new List<Order>();
}
Run Code Online (Sandbox Code Playgroud)
我在处理一对多关系时看到的EF4代码示例公开了一个公共ICollection.
有没有办法通过暴露它们来持久化和恢复我的收藏?如果没有,似乎我的域对象将被设计为满足ORM的要求,这似乎违背了努力的精神.公开ICollection(使用它的Add等方法)似乎并不特别干净,也不是我的默认方法.
更新
发现这篇文章表明它在5月份是不可能的.当然,微软的海报确实表示他们"正在强烈考虑实施"它(我希望如此)而且我们已经半年了,所以也许有一些进步?
icollection ×10
c# ×8
.net ×2
ienumerable ×2
arrays ×1
code-first ×1
collections ×1
iqueryable ×1
linq ×1
list ×1