lambda

Python one-line "for" expression

Hi! I'm not sure if i need lamda, or something else. But still, i need 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 iterate through the items and make it one-l...

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<Action> a) {} but the following overload is ambiguous: public MyClass(Action a) {} I figured using an explicit delegate would r...

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

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<short, string> programDictionary = splitProgram.ToDictionary<short, string>((value) => i++); What's...

LINQ to SQL lambda exp. OrderBy, Case When

Hi guys! 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 => t.ForumID == id) .OrderBy( t => t.Replies .OrderBy(r => r.AddDat...

Cast from filtered custom List<T> with LINQ

I have a custom list which inherits from Generic.List<T> like this: public class TransferFileList<T> : List<TransferFile> { .. } When I set (where 'Files' is a TransferFileList<T>): var files = uploadResponse.Files.Where(x => !x.Success).ToList() the 'files' object resolves as System.Collections.Generic.List<TransferFile>, not Tran...

Lambda expression common syntax-CSharp

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. ...

Lambda expression - How to work on delegates in CSharp ?

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 (pardon me for the bad syntax) var otherway= F...

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().Invoke(validator); if(!predicate.Invoke(propertyValue)) ...

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(c => new { c.cust.CustomerID, c.cust.CompanyName, ...

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"); sb.Replace("aaa", delegate() ...

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 attribute exists on the property the expression re...

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) { // get highest value of specified field in MyDatas, retur...

LINQ: help with "contains" and a Lambda query

Hi there, I have a list which contains enums, its a standard Enum but has an attribute attached to it and an extension method which returns a CHAR of the enum (see below - GetCharValue), the extension works great. Now I have (another extension method for linq) public static IQueryable<Building> WithStatus(this IQueryable<Building...

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 build a dynamic user interface from a databas...

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.FromSeconds(x), // Do it in x seconds ...

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) { OfferManager offerManager = new OfferM...

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 something interesting with the exception } } } When I run my unit test with something like the following:...

iQueryable and Expression Tree

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

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) { Expression<Func<OtherTy...

Linq - Top value form 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",TeamName="Australia", PlayerScore=334}, new T...