Все вопросы: [lambda]

518 вопросов

похожие теги: boost-lambdalambda-calculusaws-lambda
26
голосов
6ответов
20774 просмотров

Write a method which accepts a lambda expression

I have a method with the following signature: void MyMethod(Delegate d){}; void MyMethod(Expression exp){}; void MyMethod(object obj){}; However, this fails to compile: MyMethod((int a) => a) with the following error: "Cannot convert lambda expression to type 'object' because it is ...

1
голосов
3ответов
265 просмотров

Working on lambda expression

I am squaring each integer in a List. Here is the code. class SomeIntgs { List<int> newList = new List<int>(); public List<int> get() { IEnumerable<int> intrs = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; newList.AddRange(intrs); return newL...

7
голосов
2ответов
577 просмотров

What's the best way to define & access selected properties in C#?

From my recent question, I try to centralize the domain model by including some silly logic in domain interface. However, I found some problem that need to include or exclude some properties from validating. Basically, I can use expression tree like the following code. Nevertheless, I do not lik...

370
голосов
16ответов
510201 просмотров

Is there a way to perform "if" in python's lambda

In python 2.6, I want to do: f = lambda x: if x==2 print x else raise Exception() f(2) #should print "2" f(3) #should throw an exception This clearly isn't the syntax. Is it possible to perform an if in lambda and if so how to do it? thanks

4
голосов
4ответов
901 просмотров

Implicit Type Inference only for lambda expressions ? Why ? Confused !

I have the following sample code (used for C# 3.5 study purpose only !). I am making a call to the Sort function that accepts an IEnumerable and a sort function. If I call it using a lambda expression (Case A) the compiler can derive the return type TResult, however when I pass the func SortInt...

18
голосов
3ответов
14731 просмотров

What does "lambda" mean in Python, and what's the simplest way to use it?

Can you give an example and other examples that show when and when not to use Lambda? My book gives me examples, but they're confusing.

8
голосов
1ответов
8321 просмотров

Rspec Mocks: mock / yield the block from a method call

I've got this code: Net::SSH.start(@server, @username, :password => @password) do |ssh| output = ssh.exec!(@command) @logger.info 'SSH output: ' @logger.info output end I can mock the SSH.Start using RSpec's mock framework like this, to tell me th...

39
голосов
6ответов
25729 просмотров

Linq - Top value from each group

How can I employ Linq to select Top value from each group when I have a code segment like : var teams = new Team[] { new Team{PlayerName="Ricky",TeamName="Australia", PlayerScore=234}, new Team{PlayerName="Hussy",TeamName="Australia", PlayerScore=134}, new Team{PlayerName="Clark",TeamNa...

8
голосов
1ответов
3523 просмотров

Lambda Parameter not in scope -- while building binary lambda expression

When creating a lambda expression by hand I get a 'Parameter not in scope' exception. Any ideas as to what I am doing wrong? public class OtherType { public string First_Name { get; set; } public string Last_Name { get; set; } } static void Main(string[] args) ...

0
голосов
3ответов
4809 просмотров

iQueryable and Expression Tree

Can anybody explain me how to use (1) iQueryable (2) Expression Tree in C# by providing a very basic example? Both are not correlated, instead of making two separate questions, I wish to clear my doubt in a single question. Advanced Thanks.

1
голосов
2ответов
192 просмотров

Lambdas and Exception handling

I have something like the following: public class FooWrapper { public Action Foo { get; set; } public void Execute() { try { Foo.Invoke(); } catch (Exception exception) { //exception is null //do someth...

0
голосов
5ответов
1147 просмотров

How to execute a callback method instead of an anonymous method?

The following example works, but how can I change it so that instead of executing the anonymous method, it executes my existing callback method OnCreateOfferComplete()? using System; namespace TestCallBack89393 { class Program { static void Main(string[] args) { ...

3
голосов
7ответов
2322 просмотров

Best way to call a single operation at some time in the future?

I want to fire off a timer to execute once at some point in the future. I want to use a lambda expression for code brevity. So I want to do something like... (new System.Threading.Timer(() => { DoSomething(); }, null, // no state required TimeSpan.Fr...

2
голосов
2ответов
1500 просмотров

Convert C# statement body lambda to VB

It appears that VB in VS8 doesn't support/convert lambda expressions with a statement body. I'm using them in my C# application, but now must convert it to VB. I'm creating a whole bunch of controls dynamically, and I want to be able to give them event handlers on the fly. This is so I can bui...

173
голосов
6ответов
242601 просмотров

LINQ: "contains" and a Lambda query

I have a List<BuildingStatus> called buildingStatus. I'd like to check whether it contains a status whose char code (returned by GetCharCode()) equals some variable, v.Status. Is there some way of doing this, along the lines of the (non-compiling) code below? buildingStatus.Contains(item ...

3
голосов
2ответов
7085 просмотров

How write a LINQ expression that will return the maximum value of a field?

public class MyData { public int Value1 { get; set; } public int Value2 { get; set; } public int Value3 { get; set; } } public class MyViewData { List<MyData> MyDatas = new List<MyData>(); public int GetMaxValue(Expression<Func<MyData, int>> action) {...

20
голосов
2ответов
15853 просмотров

Get Custom Attributes from Lambda Property Expression

I am using ASP.NET MVC 2 Preview 2 and have written a custom HtmlHelper extension method to create a label using an expression. The TModel is from a simple class with properties and the properties may have attributes to define validation requirements. I am trying to find out if a certain attribut...

0
голосов
7ответов
2407 просмотров

How to return a type from an anonymous method/delegate

i want to return a string value from an anonymous method/delegate. How can i achieve something like this: Note this will give a compile time error: Cannot convert anonymous method to type 'string' because it is not a delegate type StringBuilder sb = new StringBuilder("aaa"); ...

1
голосов
1ответов
639 просмотров

Lambda expression conversion

I am a beginner in Linq. How can I rewrite this lambda expression as a linq compiled query? var query5 = CustomerList.Select((cust, index) => new {cust, index}) .Where(c => c.cust.Country == "USA" && c.index > 70) .Select...

0
голосов
1ответов
485 просмотров

How to get the body of a predicate?

I Have a quite simple question that I just cant figure out. The method code is simple: protected void Require<TValidator, TParam>(TValidator validator, Expression<Func<TValidator, TParam>> property, Predicate<TParam> predicate) { var propertyValue = property.Compile(...

0
голосов
5ответов
326 просмотров

Lambda expression - How to work on delegates in C#?

I took the following example from Jrista's answer to a post. Finding Twentyone count int[] numbers = new[] { 1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 3, 21 }; var twentyoneCount = numbers.Where(n => n == 21).Count(); Suppose i use "Func" delegate how can i get the count ? I tried as (par...

4
голосов
2ответов
2358 просмотров

Lambda expression common syntax

Can I follow any simple synax or rules for building "lambda expression" in C#? I read some articles and understood what a lambda expression is, but if I have the general syntax or rules that would be helpful.

3
голосов
3ответов
965 просмотров

Cast from filtered custom List<t> with LINQ</t>

I have a custom list which inherits from Generic.List&lt;T&gt; like this: public class TransferFileList&lt;T&gt; : List&lt;TransferFile&gt; { .. } When I set (where 'Files' is a TransferFileList&lt;T&gt;): var files = uploadResponse.Files.Where(x =&gt; !x.Success).ToList() the 'files' objec...

0
голосов
3ответов
4162 просмотров

LINQ to SQL lambda exp. OrderBy, Case When

Going to need your help on this one. I'm trying to OrderBy first reply datetime if present. If it's empty/null, it must order by topic datetime. I've ended up with the following expression, but it just doesn't seem to work :( return db.Topics .Where(t =&gt; t.ForumID == id) .OrderBy( t =&gt...

0
голосов
1ответов
556 просмотров

Several unusual errors when attempting to convert a string[] to a Dictionary<short string></short>

I have the following code that splits a string on newlines and converts it to a Dictionary for further processing: string[] splitProgram = program.Split(Environment.NewLine.ToCharArray()); short i = 0; Dictionary&lt;short, string&gt; programDictionary = splitProgram.ToDic...

4
голосов
3ответов
3708 просмотров

C#: Can I cast an explicit delegate to an Action delegate?

Given: delegate void Explicit(); Can I: public void Test(Explicit d) { Action a; a = d; // ???? } I have a scenario where I need to overload a constructor that has: public MyClass(Expression&lt;Action&gt; a) {} but the following overload is ambiguous: public MyClass(Action a) {...

36
голосов
6ответов
99465 просмотров

Python one-line "for" expression

I'm not sure if I need a lambda, or something else. But still, I need the following: I have an array = [1,2,3,4,5]. I need to put this array, for instance, into another array. But write it all in one line. for item in array: array2.append(item) I know that this is completely possible to i...

0
голосов
2ответов
950 просмотров

How to Remove unneccessary list using lambda and functional C# paradigm

Hello Functional C# Friends, So this time i am trying to compact my code and write in more functional , lambda style, as well as i would like to avaoid creating unneccessary lists and classes and let compiler do the work for me. I did manage to convert some small piece of code in functional way ...

0
голосов
2ответов
1224 просмотров

how to convert multiple linq statement to selectmany

I am a beginner with LINQ and lambda function's. I was wondering how can i convert multiple linq statement to one statement using selectmany function. string viewname = (from a in StoredProcedureParameters.Tables[0].AsEnumerable() where a.Field&lt;string&gt;("ViewName") == displ...

34
голосов
5ответов
10270 просмотров

FindAll vs Where extension-method

I just want know if a "FindAll" will be faster than a "Where" extentionMethod and why? Example : myList.FindAll(item=&gt; item.category == 5); or myList.Where(item=&gt; item.category == 5); Which is better ?