这是我正在使用的简化版本,只是为了让您知道我想要完成的任务:
public Dictionary<int, Func<int>> Magic(Dictionary<int, int> dictionary)
{
return dictionary
.Select(v => new
{
key = v.Key,
function = new Func<int>(() => v.Value + 5) // *
})
.ToDictionary(v => v.key, v => v.function);
}
Run Code Online (Sandbox Code Playgroud)
是否可以将标有(*)的线缩短一些?因为当我尝试删除多余的(在我看来)委托创建时,它会给出错误:
function = () => v.Value + 5 // *
Run Code Online (Sandbox Code Playgroud)
但它不应该给出错误,很清楚返回的类型是什么......
主要问题是“是否可以将任何类型的 func 作为参数以及如何传递?”。我正在学习 Go 并希望像这样制作我自己的异步包装函数:
func AsyncFunc(fn func(), args ...interface{}) chan bool {
var done chan bool;
go func() {
fn(args...);
done <- true;
}();
return done;
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
max := func(a, b int) int {
//some hard code what will be goroutine
if a > b {return a};
return b;
}
done := AsyncFunc(max, 5, 8);
//some pretty code
<- done;
Run Code Online (Sandbox Code Playgroud)
PS如果我的英语不好,对不起...
Edit1:我知道它是无用的、缓慢的和危险的。这只是我想要实现的疯狂想法。
我有一个使用 Func 参数的单元测试,我似乎无法使用最小起订量。查看 StackOverflow 上 Func 参数的其他示例,我认为这应该有效:
要测试的代码:
public class PatternManager : IPatternManager
{
private readonly ICacheManager _cacheManager;
private readonly IDataRepo _data;
public PatternManager(ICacheManager cacheManager, IDataRepo dataRepo)
{
_cacheManager = cacheManager;
_data = dataRepo;
}
public List<Pattern> GetAllPatterns()
{
var allPatterns = _cacheManager.Get(() => _data.GetAllPatterns(), nameof(_data.GetAllPatterns));
return allPatterns;
}
public Pattern GetBestPattern(int[] ids)
{
var patternList = GetAllPatterns().Where(w => w.PatternId > 1);
... More code...
}
...
}
public interface ICacheManager
{
T Get<T>(Func<T> GetMethodIfNotCached, string cacheName = null, int? …
Run Code Online (Sandbox Code Playgroud) 我有一个数值分析程序,为简单起见计算类似于以下的算法:
y = ax^3 + bx^2 + cx + d;
Run Code Online (Sandbox Code Playgroud)
我在运行时计算a,b,c,d的值,并希望将以下等效项传递给a Func<double, double>
.我可以在哪里设置X的值,并获得Y.
y = 12x^3 + 13x^2 + 14x + 15;
Run Code Online (Sandbox Code Playgroud)
其中12,13,14,15是运行时计算的数字.
我意识到这可以通过传入一个双数组来完成,就像这样:Func<double[], double>
但我试图避免传递常量(可能很多).
有没有办法在运行时在func中设置这些数字?
(最好不要计算func <>本身的a,b,c,d部分?a,b,c,d的计算是工作的80%)
例如:
a = ...
b = ...
c = ...
Func<x, double> {
((const)a) * x^3 + ((const)b) * x^2 + ((const)c) * x + 15;
}`
Run Code Online (Sandbox Code Playgroud)
对于ABCD的每次评估 - 我将评估10 x.
我正在使用System.Func但是已经遇到了绊脚石.
System.Func<TReturn> // (no arg, with return value)
System.Func<T, TReturn> // (1 arg, with return value)
System.Func<T1, T2, TReturn> // (2 arg, with return value)
System.Func<T1, T2, T3, TReturn> // (3 arg, with return value)
System.Func<T1, T2, T3, T4, TReturn> // (4 arg, with return value)
Run Code Online (Sandbox Code Playgroud)
它接受的最大值是4个参数.
有没有办法将此扩展到5个参数?
我有一个多线程应用程序,它在BlockingCollection队列上创建一个字符串列表,我想获取该字符串列表并将其转换为一个或两个步骤中的项目对象集合
是否可以创建一个func <>或lamda方法来实现这种类型的结果
public class item
{
public string name { get; set; }
public item(string nam)
{
name = nam;
}
}
IList<string> alist = new string[] { "bob","mary"};
Run Code Online (Sandbox Code Playgroud)
你在哪里获取类型字符串的Ilist <>或IEnumerable <>并返回IList
所以对于单项Func <>
Func<string, item> func1 = x => new item(x);
Run Code Online (Sandbox Code Playgroud)
但标签看起来很像
Func<IEnumerable<string>,IList<item>> func2 = x=> x.ForEach(i => func1(i));
Run Code Online (Sandbox Code Playgroud)
我试图在sqaure孔中放置一个圆形挂钩,或者我的语法/逻辑是错误的
提前致谢
public class EcImageWrapper
{
//etc...
public IQueryable<EcFieldWrapper> ListOfFields
{
get
{
//logic here
return this.listOfFields.AsQueryable();
}
}
public EcFieldWrapper FindBy(Expression<Func<EcFieldWrapper, int, bool>> predicate)
{
return this.ListOfFields.Where(predicate).SingleOrDefault();
}
public EcFieldWrapper FindByName(string fieldName)
{
return this.FindBy(x => x.Name.ToUpper() == fieldName.ToUpper());
//error here, wanting 3 arguments
//which makes sense as the above Expression has 3
//but i don't know how to get around this
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,表达式>要求我使用3个参数,在过去我只使用2作为有问题的实例.但是,现在我想在这个实例中对一个集合进行查找.
我收到以下错误:
Delegate 'System.Func<MSDORCaptureUE.Wrappers.EcFieldWrapper,int,bool>' does not take 1 arguments
The best overloaded method match for 'CaptureUE.Wrappers.EcImageWrapper.FindBy(System.Linq.Expressions.Expression<System.Func<CaptureUE.Wrappers.EcFieldWrapper,int,bool>>)' has some …
Run Code Online (Sandbox Code Playgroud) 难以用语言解释,所以我将提供一个例子.
我想在Razor中输出这个(但是它的剃刀是无关紧要的,我的问题是关于c#):
@SomeClass.SomeClass.SomeClass.ID.ToString()
Run Code Online (Sandbox Code Playgroud)
任何SomeClass都可以为null(这是一个外部api,我实际上没有影响)
所以我尝试了这个:
@functions{
private string Safe(Func<string> val, string defaultValue)
{
try
{
return val.Invoke();
}
catch(NullReferenceException ex)
{
return defaultValue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后这个:
@Safe(SomeClass.SomeClass.SomeClass.ID.ToString, "value not found")
Run Code Online (Sandbox Code Playgroud)
但是没有雪茄......有没有办法实现这一目标?谢谢.
UPDATE1:使用NullReferenceException显然要多得多.
我从我的请求中收到此参数:
sort=homeCountry
Run Code Online (Sandbox Code Playgroud)
我需要按照发送到sort
参数的任何列进行排序,因此我创建了一个类似的方法:
string sort = "homeCountry";
Func<PaymentRateTrip, string> orderBy = (
x => sort == "homeCountry" ? x.HomeCountry :
sort == "hostCountry" ? x.HostCountry :
sort == "homeLocation" ? x.HomeLocation :
sort == "hostLocation" ? x.HostLocation :
x.HomeLocation
);
Run Code Online (Sandbox Code Playgroud)
这工作正常.
但是,上面的列都是字符串.但是,我还需要添加十进制列,如下所示:
string sort = "homeCountry";
Func<PaymentRateTrip, string> orderBy = (
x => sort == "homeCountry" ? x.HomeCountry :
sort == "hostCountry" ? x.HostCountry :
sort == "homeLocation" ? x.HomeLocation :
sort == "hostLocation" ? x.HostLocation :
sort …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为字典(任何数据类型)创建一个通用函数.我调用函数时遇到此错误:
错误:无法将类型'[Int:String]'的值转换为预期的参数类型'[_:_]'wprintgeneric(inp:w)^
我的代码如下:
var w=[1:"wisam",2:"khlaid",3:"abd"] //dictionary
func wprintgeneric<T>(inp:[T:T]){
for (i,j) in inp{print(i,j)}
}
wprintgeneric(inp: w)
Run Code Online (Sandbox Code Playgroud) func ×10
c# ×8
linq ×4
delegates ×2
lambda ×2
.net-3.5 ×1
dictionary ×1
generics ×1
go ×1
moq ×1
parameters ×1
runtime ×1
swift ×1
unit-testing ×1