我的第一篇(也是非常可怕的帖子)如下。
我尝试做一个完整的例子,我想要得到什么。我希望这能得到更好的解释。
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Boy> boys = new List<Boy>();
boys.Add(new Boy("Jhon", 7));
boys.Add(new Boy("Oscar", 6));
boys.Add(new Boy("Oscar", 7));
boys.Add(new Boy("Peter", 5));
ClassRoom myClass = new ClassRoom(boys);
Console.WriteLine(myClass.ByName("Oscar").Count); // Prints 2
Console.WriteLine(myClass.ByYearsOld(7).Count); // Prints 2
// This has errors...................
// But this is as I would like to call my BySomeConditions method....
Console.WriteLine( // It should print 1
myClass.BySomeConditions([myClass.ByName("Oscar"),
myClass.ByYearsOld(7)]
)
);
Console.ReadKey();
}
class ClassRoom
{
private …Run Code Online (Sandbox Code Playgroud) 我试过:
from sqlalchemy import VARCHAR
result = session.query(Table).filter(func.convert(VARCHAR(8), Table.datetimefiedld, 8) >= some_date).all()
Run Code Online (Sandbox Code Playgroud)
我收到 AttributeError: 'VARCHAR' 对象没有属性 'self_group'
有人可以解释一下如何在 sqlalchemy 中使用 CONVERT 函数吗?谢谢!
如何使用FakeItEasy框架检查Func是否被调用?
例子:
Func<bool> myFunc = () => true;
// Unfortunately this fails:
A.CallTo(myFunc.Invoke()).MustHaveHappened();
Run Code Online (Sandbox Code Playgroud) 我怎样才能将 lambda 表达式写入optionObject.Forms.First(f => f.FormId == formId).MultipleIterationFunc 所以最后我有类似的东西
Func<FormObject, bool> FormID = f => f.formID == passedVal;
Run Code Online (Sandbox Code Playgroud)
然后在第一个表达式上使用它来得到类似的东西
optionObject.Forms.First(FormID).MultipleIteration
Run Code Online (Sandbox Code Playgroud)
我试过
Func<FormObject, PassedVal, bool> FormID => formID == PassedVal;
Run Code Online (Sandbox Code Playgroud)
但没有用。
请注意, lambda 表达式没有任何问题,它工作得很好。我只是想创建一个函数来用函数名称替换表达式,以使代码看起来更短且可维护。
我一直在重构我的项目中的一个常见模式,发现它不像使用 LINQSelect到异步函数那么简单。
对于上下文,这是当前的完成方式。
async Task<ICollection<Group>> ExecuteQueryGroupsForDomain(DomainInfo domain, int batchSize, CancellationToken ct)
{
try
{
return await BlahBlahActuallyGoGetGroupsForDomainHere(domain, batchSize, ct);
}
catch (Exception e)
{
return null;
}
}
var executeQueries = new List<Func<CancellationToken, Task<ICollection<Group>>>>();
domains.ForEach(domain =>
executeQueries.Add(async (ct) =>
await ExecuteQueryGroupsForDomain(domain, 123, ct)));
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试ForEach使用 LINQ替换循环部分:
var executeQueries = domains.Select(domain =>
async (ct) => await ExecuteQueryGroupsForDomain(domain, 123, ct));
Run Code Online (Sandbox Code Playgroud)
它抱怨Type arguments cannot be inferred by the usage让我相信我没有从 返回任何东西Select,但我显然正在返回Func我想要的。
有没有更好的方法来创建Func's列表,最好避免显式强制转换?当Select …
我在oracle中对架构,用户和功能id感到很困惑.让我考虑两种不同的情况
案例I:
让我们考虑SCOTT@ORCL.如果我们认为SCOTT是用户.在创建用户时,它将创建一个模式.纠正我如果我错了.在这种情况下,当我们创建SCOTT用户时,就创建了SCOTT模式.假设我们创建另一个模式说X. 这可能是SCOTT用户拥有X架构吗?
案例二:
让我们考虑SCOTT@ORCL.如果我们认为SCOTT是独立的模式,即仅由模式命令创建.如果是这样,那么对于任何将拥有它的用户来说,什么是模式的使用.
我听说oracle函数ID是一个连接多个用户/模式(我不知道我是否可以将schema/user放在这里)的数据库.b/w oracle功能ID与用户/架构有区别吗?
在具有各种Func委托重载的函数中使用匿名方法时,我在Visual Studio 2010中遇到了一些奇怪的行为.
我在下面创建了一个小型复制课程.
考虑这个ListViewAdapter类
namespace LambdaTestApp
{
public class ListViewAdapter<T>
{
private Func<int, string, int, string> _converter1;
private Func<RefType1, string, string> _converter2;
public ListViewAdapter(int arg1, Func<int, string, int, string> converter)
{
_converter1 = converter;
}
public ListViewAdapter(int arg1, Func<RefType1, string, string> converter)
{
_converter2 = converter;
}
public static ListViewAdapter<T> MockPopulate(int arg, Func<int, string, int, string> converter) {
ListViewAdapter<T> instance = new ListViewAdapter<T>(arg, converter);
return instance;
}
public static ListViewAdapter<T> MockPopulate(int arg, Func<RefType1, string, string> converter)
{
ListViewAdapter<T> instance …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
static Func<object, string> s_objToString = (x) => x.ToString();
static Func<string, string> s_stringToString = s_objToString; //compiles
static Func<int, string> s_intToString = s_objToString; //error
Run Code Online (Sandbox Code Playgroud)
第二行编译但第三行无法编译并出现错误:
无法隐式将类型'
System.Func<object,string>' 转换为'System.Func<int,string>'
这是为什么?
据我所知,遗传学,尽管字符串是从对象派生List<string>不会得到List<object>,但在这里object,以string作品和object以int失败了,为什么?
好吧,让我说我理解为什么; 现在的问题是有办法解决它(其他然后定义MyInt类框int,因为Func<object,string>对Func<MyInt,string>作品)?
我正在尝试将谓词传递给函数,但是它没有输入。我实际上正在尝试将计算延迟到发出呼叫为止。
有没有办法使用c#谓词类型呢?如果不是为什么。
我知道使用Func可以做到这一点
Func<bool> predicate = () => someConditionBool;
Run Code Online (Sandbox Code Playgroud)
但是我想这样做:
Predicate<> predicate = () => someConditionBool;
Run Code Online (Sandbox Code Playgroud) 我在单元测试中遇到了关于处理DateTime.Now的问题的接受答案,其中包含以下代码示例:
private readonly Func<DateTime> _nowProvider;
public SomeClass(Func<DateTime> nowProvider)
{
_nowProvider = nowProvider;
}
public bool Foo()
{
return (_nowProvider().DayOfWeek == DayOfWeek.Sunday);
}
Run Code Online (Sandbox Code Playgroud)
实例化如下:
var s = new SomeClass(() => DateTime.Now);
Run Code Online (Sandbox Code Playgroud)
我Func<T>在C#中使用的并不多,所以我想我会看看它的Microsoft文档,它有以下几点:
您可以使用此委托表示可以作为参数传递的方法,而无需显式声明自定义委托.封装的方法必须对应于此委托定义的方法签名.这意味着封装的方法必须没有参数,并且必须返回一个值.
为什么在示例中传递一个Func<DateTime>实例化Class(() => DateTime.Now)的构造函数会更有益
而不是简单地传入一个DateTime实例化为Class(DateTime.Now)构造函数的参数?
根据上面提到的Microsoft文档,LINQ lambda构造函数也接受了Func<T>论证,我的经验证明它们非常灵活,但我不明白为什么?
func ×10
c# ×8
.net ×3
generics ×2
lambda ×2
linq ×2
covariance ×1
fakeiteasy ×1
oracle ×1
python ×1
schema ×1
select ×1
sqlalchemy ×1