我已经找到了Get/Set缓存项的代码,但是我不知道如何调用这个方法,例如。如何将适当的Func<T> getData传递给这个方法?
public class Cache<T> : MemoryCache where T : class
{
public void Set(string cacheKey, T cacheItem, CacheItemPolicy policy = null)
{
//...
}
public bool TryGet(string cach
我试图让所有带有子子程序的子MenuItems一直到结束,但是这段代码只得到第一级。
如何将此代码转换为管理该功能?
public class MenuItem : IEntityBase
{
public int Id { get; set; }
public string Text { get; set; }
public virtual ICollection<MenuItem> Children { get; set; }
public virtual MenuItem Parent { get; set; }
public bool
这样可以吗?
public class BaseRepository<T> where T : class
{
private readonly DbContext _dbContext;
public BaseRepository(DbContext dbContext)
{
_dbContext = dbContext;
}
public T Get(Func<T, bool> predicate)
{
return GetAll(predicate).FirstOrDefault(
我正在创建一个扩展泛型方法Apply,它做什么并不重要,主要的问题是我不能理解如何测试它,像ApplyOnNullFunctionThrow这样的简单测试不能说明没有抛出异常,但是如果我从main运行一个“手动测试”,它将抛出 主要内容: class Program
{
static void Main(string[] args)
{
foreach (var i in first.Apply(second,f)) Console.WriteLine("{0}", i);
Console.ReadLine();
}
}
我有一节课
public static class MyClass
{
public static T MyMethod<T>(T item) where T : ISomeInterface<T>, new
{
return MyMethod(new[] { item}).First();
}
public static IEnumerable<T> MyMethod<T>(params T[] items) where T : ISomeInterface<T>, new
为什么下面的X方法没有被调用?!
static class Program
{
private static void Main()
{
X((IEnumerable<int>)null);
}
public static IEnumerable<T> X<T>(IEnumerable<T> e)
{
if (e == null)
throw new ArgumentNullException();
yield break;
}
我已经编写了一个函数,它将接受一个通用的IEnumerable,并将这些对象(如果可能的话)映射到我想要用于算法的对象。我对泛型对象或IEnumerable没有太多的经验,所以我想征求一些建议。
我写了这段代码:
public static IEnumerable<OtherObject> MyObjectToOtherObject<T>(IEnumerable<T> objects)
{
if (objects.GetType() == typeof(MyObject))
{
var data = (IEnumerable&
我经常发现自己用列表和其他集合类型来做这个(或类似的)工作:
if (someList != null && someList.Count > 0)
//take some action on the list
因此,我变得懒惰,并编写了以下扩展方法来帮助我做到这一点:
public static bool IsNullOrEmpty(this IEnumerable self)
{
//Convert to collection here because IEnumerable doesn't have a coun
我使用实体框架6为MVC项目划分了一个单独的类。
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
}
继承如下
public class Repository<T> : IRepository<T> where T : class
{
protected readonly DbContext _context = null;
private readonly DbSet<T> _entities;
p
给出下面的代码,如何将expected作为IEnumerable<T>返回?
public List<object> expected = new List<object>();
public IEnumerable<T> SqlQuery<T>(string sql)
{
// Return expected as IEnumerable<T>
}
(这是为单元测试创建的类,用于模拟实体框架SqlQuery<T>方法。我们可以提前设置我们的预期结果,并让它返回您期望的结果。)
我需要从懒惰的源中提取东西,直到以这种方式达到超时为止:
var results = lazySource.SelectMany(item =>
{
//processing goes here
}).Pull(timeout: TimeSpan.FromSeconds(5), times: maxNumberOfIterations);
我有一个解决方案:
public static class IEnumerableExtensions
{
public static IEnumerable<T> Pull<T>(this IEnumerabl
注意,这个问题有点微妙,所以仔细阅读它:我不只是想找出某个工件类型是否实现了IEnumerable:
下面是我用初始实现编写的一个函数:
// is "toType" some sort of sequence that would be satisfied
// by an array of type T? If so, what is the type of T?
// e.g.
// getArrayType(typeof(string[]) == typeof(string)
// getArrayType(typeof(IEnu
我有一个.dll和一个控制台应用程序,它使用上述.dll,但不直接引用,而是通过反射加载。控制台应用程序调用.dll中一个类的方法。
方法签名为IEnumerable<Customer> GetAll();
在.dll中,我这样做了:
class CustomerRepository : ICustomerRepository
{
public IEnumerable<Customer> GetAll()
{
using (var db = new DB02Context())
{
List<
我已经看过很多关于ToList和泛型约束的堆栈溢出Q&,但是我还没有找到一个解释下面最后返回中的“语法错误”的方法。为什么我必须显式地Select和cast元素("B")?
public interface I1
{
}
public class C2 : I1
{
public static List<I1> DowncastListA( List<C2> list )
{
// "A": A concrete class works.
return list == null ?
我的桌子如下所示,用的是Sqlite:
public class Medication
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string unique_id { get; set; }
public string username { get; set; }
public string insulin_type { get; set; }
public string units { get; set; }
public string status
这就是我希望我的类看起来的样子,但是这段代码不会编译。我该怎么做呢?
public interface ISomeInterface
{
string AMember { get; }
}
public abstract class BaseClass
{
public abstract ISomeInterface AObject { get; }
public abstract IEnumerable<ISomeInterface> AMethod();
}
public class DerivedClass<T> : BaseClass
下面的代码有什么问题?即使数据库中存在匹配的记录,它也不会返回任何项。如果错误,如何将我的IQueryable转换为IEnumerable
public IEnumerable<TimelineItem> TimeLineItems { get; set; }
public IEnumerable<TimelineItem> GetTimeLineItems(int SelectedPID)
{
TimeLineItems = (from t in db.TimelineItems
where t.ProductID =
我试图按CategoryId分组,并显示CategoryId +组的Name属性,我需要帮助修改这个代码,这样就可以显示Name属性,查看下面的视图,看看我的意思。
数据库
ItemCategory
int ItemId
int CategoryId
范畴
int CategoryId
int?ParentId
字符串名
var itemcategories = db.Fetch<ItemCategory, Category>(@"SELECT * FROM ItemCategory LEFT JOIN Category on Category.CategoryId = I
当T直到运行时才知道时,我如何将IEnumerable转换为IEnumerable<T>?
我有一个方法,IEnumerable<T>绝对需要一个,其中T直到运行时才知道,和必须是实际的类型,而不是像object那样的祖先类型。
我打赌反射API允许这样的东西,但我不知道具体是如何实现的。
更新
我可以得到这样一个类型对象:
var first = results.Cast<object>().FirstOrDefault();
if (first != null)
{
var type = first.GetType();
}
Update2
我
代码
public class SQL
{
public static string ConnectionString = "";
public static IEnumerable<T> ExecuteQuery<T>(string Queary, string ConnectionString)
{
try
{
using (MySqlConnection Connection = new MySqlConnection(ConnectionString))
可以在列表扩展方法中使用IEnumerable FirstOrDefault吗?我收到错误消息‘列表不包含FirstOrDefault的定义’?
using LanguageExt;
using System;
using System.Linq;
public static class Question
{
public static Option<T> Lookup<T>(this List<T> enumerable, Func<T, bool> predicate)
{
if (enumerable
我写了一个自定义扩展,它返回两个集合以及a和b中的两个唯一元素之间的交集。 public static void CompareTo<T>(
this IEnumerable<T> sourceA,
IEnumerable<T> sourceB,
IEqualityComparer<T> equalityComparer,
out ICollection<T> commonElements,
out ICollection<T> elementsOnlyInSourceA,
你能帮我纠正这个代码吗?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace NTSoftHRM
{
// ------------------------------------------------------------------------
public class EnumValueList<T> : IEnumerable<T>
{
// ---
我正在尝试在C#中使用monoDevelop编写一个自定义的LinkedList类,只是为了测试和学习。下面的代码永远不会编译,我也不知道为什么!它甚至都没告诉我出了什么问题。它所说的就是:错误:编译器似乎崩溃了。有关详细信息,请查看构建输出板。当我去检查输出板,它也没有帮助:未处理的异常: System.ArgumentException:指定的字段必须在泛型类型定义中声明。参数名称:字段
我能做什么?
using System;
using System.Text;
using System.Collections.Generic;
namespace LinkedList
{
一个相当基本的变化问题。给定这样的类:
public class X
{
public T A;
public T B;
public T C;
...
// (other fields, properties, and methods are not of interest here)
}
我正在寻找一种简洁的方法来编写一个方法,该方法将返回可枚举集合中所有非空的A、B、C、...。(假设将这些字段声明为数组不是一个选项。)
public IEnumerable<T> GetAllNonNullABCs(this X x)
{
/