遇到存储库模式的问题以及抽象类的使用.
我有一个存储库,它实现了一个返回抽象类型的ICollection的方法.
这是我的抽象类:
public abstract class Location
{
public abstract string Name { get; set; }
public abstract LocationType Type { get; }
}
Run Code Online (Sandbox Code Playgroud)
这是抽象类的具体实现:
public class Country : Location
{
public override string Name { get; set; }
public override LocationType Type { get { return LocationType.Country; } }
}
Run Code Online (Sandbox Code Playgroud)
这是我的存储库:
public class LocationsRepository : Locations.Repository.ILocationsRepository
{
public ICollection<Location> GetAllLocations()
{
Country america = new Country { Name = "United States" };
Country …Run Code Online (Sandbox Code Playgroud) 我有一个ICollection<SomeClass>.
public class SomeClass
{
public string Text { get; set; }
public bool IsPreferred { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
那里的物品已经预订,所以"下一步"确实意味着什么.
在我的场景中,序列的内容如下所示:
[0] - "a",假
[1] - "b",是的
[2] - "c",假
我试图在那个之后获得"下一个"元素IsPreferred == true.所以在上面,我想得到元素2,我想清除其他IsPreferred值.
所以我想最终得到这个:
[0] - "a",假
[1] - "b",假
[2] - "c",是的
基本上将首选项目洗牌.
最好的方法是什么?我唯一能想到的是创建一个新数组,逐个添加它们,跟踪首选的索引,然后在上述索引+1处获取元素.
有更好的想法吗?
鉴于以下情况:
var collection = myEnumerable as ICollection<MyType>;
Run Code Online (Sandbox Code Playgroud)
引擎盖下会发生什么?ICollection有一个伯爵财产.这个演员是否枚举了可枚举的计数或更多涉及的事情?
我遇到了一个问题,我试图模拟一个包含Items类型属性的对象ICollection<>.我收到以下错误:
System.NotSupportedException:非虚拟(在VB中可覆盖)成员上的无效设置:m => m.Items
问题是属性Items已经是虚拟的.
我在下面的Sandbox中重现了我在项目中遇到的错误:
public class ItemList
{
public virtual int Id { get; set; }
}
public class SpecialList
{
public virtual string Token { get; internal set; }
public virtual ICollection<ItemList> Items { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试SetupProperty这样时,在我的测试中发生了这个错误:
[TestFixture]
public class TestSpecialList
{
[Test]
public void Mocking_Virtual_ICollection()
{
var mockModel = new Mock<SpecialList>();
var listItem = new List<ItemList> {new ItemList {Id = 007}};
mockModel.SetupProperty(m => m.Items, listItem); …Run Code Online (Sandbox Code Playgroud) 假设我有一个ICollection<SomeClass>。
我有以下两个变量:
SomeClass old;
SomeClass new;
Run Code Online (Sandbox Code Playgroud)
如何使用来实现类似以下的功能ICollection<SomeClass>?
// old is guaranteed to be inside collection
collection.Replace(old, new);
Run Code Online (Sandbox Code Playgroud) 我有一个清单:
List<int> list = new List<int> {1, 2, 3, 4, 5};
Run Code Online (Sandbox Code Playgroud)
如果想获得我的List的字符串表示.但代码list.ToString()返回"System.Collections.Generic.List'1[System.Int32]"
我正在寻找这样的标准方法:
string str = list.Aggregate("[",
(aggregate, value) =>
aggregate.Length == 1 ?
aggregate + value : aggregate + ", " + value,
aggregate => aggregate + "]");
Run Code Online (Sandbox Code Playgroud)
得到 "[1, 2, 3, 4, 5]"
是否有标准的.NET方法用于良好的字符串格式的演示ICollection?
我有以下代码:
foreach(string reelid in unValidatedFeedersOnMachine.Keys)
{
_sqlString.Append("CompID = '").Append(reelid).Append("' ");
}
Run Code Online (Sandbox Code Playgroud)
我需要在每个迭代中添加该循环,.Appened("or ")除了最后一个.
知道我怎么知道我什么时候在这里的最后一次迭代?
为什么有List<T>.Contains(T)方法但没有List<T>.Find(T)方法?仅Find支持支持谓词的s.如果我们与它的ID属性值填充T的现有实例(但缺少其他属性),为什么我们不能在提供该对象实例的搜索搜索List,特别是当我们已经实现了定制IEquatable<T>的T,并想用什么东西在那里.但实际上,我们不能,我们必须重复我们IEquatable在Find(predicate)调用中实现的所有内容.
我试图绕过为什么IDictioary<TKey, TValue>.Values属性是类型ICollection<TValue>而不是IEnumerable<TValue>.该Values属性的.NET实现显式实现了接口属性,然后将该Values属性公开为类型Dictionary<TKey, TValue>.ValueCollection,当您尝试从中添加或删除值时会抛出异常.
这似乎是一个完整的黑客.由于字典的性质,创建一个允许您获取字典中的值集合并从中添加/删除值的实现是否有意义?
所以我想知道是否有人会对我在这里做了什么做了非常彻底的解释,我知道我在做什么,以及代码的含义是什么,但是,如果我要解释它,我会毫无头绪.
public static IEnumerable<TSource> VisitorWhere<TSource>(this IEnumerable<TSource> enumerable, Predicate<TSource> CompareMethod)
{
ICollection<TSource> temp = new List<TSource>();
foreach (TSource item in enumerable)
{
if(CompareMethod(item))
{
temp.Add(item);
}
}
return temp;
}
Run Code Online (Sandbox Code Playgroud) icollection ×10
c# ×9
.net ×5
ienumerable ×3
linq ×3
.net-2.0 ×1
c#-4.0 ×1
casting ×1
collections ×1
dictionary ×1
foreach ×1
iequatable ×1
list ×1
moq ×1
nunit ×1
predicate ×1
string ×1