.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Linq: how to share parameters between lambda expressions ?

Posted By: Rahul     Posted Date: October 25, 2009    Points: 2   Category :.NET Framework
When using Linq to objects, you will quickly feel the need to pass some parameters from a method to another but it's not so easy because each Linq method is not calling the following one. In a Linq sequence, each method is using the result computed by the previous one. So, local contexts are not visible from one method to another.
The compiler is using two technical different ways to let parameters go out of a method.

View Complete Post

More Related Resource Links

What is Lambda Expressions In LINQ

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:

change to lambda in linq


Hello my friends I tried to change this code to lambda but I could not pleaaaase help me:


  var inforeport = from m in db.Products where m.UnitPrice > info select new { unit=m.UnitPrice,name= m.ProductName };


Basic Instincts: Lambda Expressions


Sample the basic concepts of lambda expressions, explore their benefits, and witness how to use them to write more expressive programs.

Timothy Ng

MSDN Magazine September 2007

displaying parameters through expressions at the top of the report

Hi,   We developed around 70 ssrs reports for our new application.Its like,all the report names are menrioned in the left hand side and if we click it,.Net calls the SSRS reports and shows in the Right hand side.    At the top of Each and every report,we dispaly all the parameters using expressions. Now, the issue is, in the parameter if they select a single value, then it should display and if they select more than one, it should display like 'More Parameters are selected'. can you helpme how to do that?

Creating Dynamic Expressions for Lambda IQueryable Expressions



I have a ListBox that contains userNames which is inputted by a user during run time. This number of items in the ListBox is dependent on the user's input. These userNames will then be used in a Lambda expression like this:

var vHandleResult = vHandleResult.Where(a => a.UserName.Contains("userName"));

This works fine when there is only 1 user in the ListBox but doesn't produce the correct results when there are multiple items in the listBox. I tried manually creating the expression which should be written as this:

var vHandleResult = vHandleResult.Where(a => a.UserName.Contains("userName1") || a.UserName.Contains("userName2") || a.UserName.Contains("userName3") || a.UserName.Contains("userName4"));

I tried forming the "a.UserName.Contains("userName") expression as a string which is dependent on the number of the items (using foreach) and then just plug in the resulting string in the expression but it seems that it doesnt accept string as an expression.

Can someone help me identify the best approach for this?

Thanks in Advance!


How to call a Stored procedures with varchar output parameters in LINQ


I have a stored procedure with the following structure:

CREATE procedure EditFeriodigiesForm
@EditID int,
@producer varchar(50) output,
@IssuedDate datetime output,
@stadio varchar(50) output,
@Method varchar(50) output,
@Monades_N varchar(50) output,
@Monades_P varchar(50) output,
@Monades_K varchar(50) output,
@comments varchar(100) output,
@responsible varchar(50) output
Begin Try
	Begin tran
SELECT @IssuedDate=IssuedDate,@producer=ProducerID,@stadio=KalliergitikoStadio,@Method=Method,@Monades_N=Monades_N,@Monades_P=Monades_P,@Monades_K=Monades_K,@comments=Comments,@responsible=Responsible from ferti_odigies WHERE FertOdigiesID=@EditID
end try
	Begin catch
	if @@error>0
declare @ErrMsg varchar(4000),
		@ErrSeverity int
Select @ErrMsg=Error_Message(),

Raiserror(@ErrMsg ,@ErrSeverity,1)

end catch 

Now i need to call it in LINQ.I have done the mapping thing and the SPROC is ready to be called in my form.aspx.cs form, as following:
  int id = Edit_id;
            int? producer;
            DateTime date =Convert.ToDateTime(txtdate.Text);
            string stadio;
            string method="";
            string monadesn="";
            string monadesp="";
            string mo

"Like", "In" Expressions in Lambda statement


Salam to all,

for my Lambda, i need to use Like Experssion, but this give me error, i dont know how to make this.

context.CreateObjectSet<ReportsManager_Entity>().Where(u => (u.ReportAdminInt like 123)).ToList();

also i need to use "in" expression (the same of 'in' experssion in sql) but also this give me an error

context.CreateObjectSet<ReportsManager_Entity>().Where(u => (u.ReportAdminInt in [1,2,3])).ToList();

please help me,


Setting where parameters within Linq DataSource Control Markup


Hi everyone!

I've got a Linq datasource hitched up to a Formview control for clients to edit some data.  I'm having trouble setting the where parameters withing the control so it show the right set of data.

Here's the markup:

 <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext"
            TableName="ClientComments" EnableUpdate="True">
                <asp:SessionParameter DbType="Guid" Name="userID" SessionField="userGUID" />
                <asp:Parameter DefaultValue="1" Name="Product.productCategoryID" Type="Int32" />
                <asp:Parameter DefaultValue="True" Name="Product.active" Type="Boolean" />

Upon running right now, it's completely ignoring the where parameters.  What am I missing here?

which parameters should use to call linq stored procedur ?



I have JQgrid that it binds from linq using linq datasource,I want to know how can I  delete,edit,add button in JQGrid using Stored procedure.Actually I write stored procedures i just want to know what parameters pass to them and how?

here is my sp:

ALTER PROCEDURE dbo.AddStoredProcedure

@BookID int,
@MenuID int,
@Name nvarchar,
@regDate date,
@Author nvarchar,
@Publisher nvarchar,
@Price int,
@Description nvarchar,
@Status nvarchar
  Insert into Book(BookID,MenuID,[Name],RegDate,Author,Publisher,Price,Description,Status) VALUES (@BookID,@MenuID,@Name,@RegDate,@Author,@Publisher,@Price,@Description,@Status)
Delete sp

ALTER PROCEDURE dbo.DelStProcedure

  @BookID int
	AS Delete from Book where BookID=@BookID
Edit sp

ALTER PROCEDURE dbo.EditStoredProcedure

  @Name nvarchar,
  @BookID int
  UPDATE Book  SET [Name]=@Name WHERE BookID=@BookID


when i want to call these procedures i write this cod in c#:

 protected void JQGrid2_RowDeleting(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)

        DataClassesDataContext dc = new DataClassesDa

Delegates and Lambda Expressions


What, exactly, relationship between Delegates and Lambda Expressions? More specifically, why is using a Lambda Expression in place of a Delegate allowed?

Playing with Linq grouping: GroupByMany?

One of its features is grouping. Many people understand grouping like it is defined in Sql. Linq is implementing grouping quite the same way. Let's discover this syntax and how to make consecutive groups easier.

LINQ : Implementing IN and NOT IN

I got tried of typing

var result = from s in source
where items.Contains(s)
select s;and so I implemented the IN and NOT IN methods as extension methods:

101 LINQ Samples Tutorials

101 LINQ Example with sample code snippets....This sample prints each element of an input integer array whose value is less than 5. The sample uses a query expression to create a new sequence of integers and then iterates over each element in the sequence, printing its value

Linq to Sql CompiledQuery container

Ok now let's go. Here is just a little trick but with some interesting patterns that could be useful in some other contexts not connected to Linq to Sql.

When using Linq expressions, like with Linq to Sql, translating the expression into something else (sql for example) is taking time and resources. Sometimes it's negligible, sometimes not...

LINQ to SQL Paging using GridView in C# and ASP.NET 3.5

This tutorial will show you how we can extend the LINQ to SQL Class and create methods that will allow us to page data from a SQL database. C# version.

Various SQL Expressions

A comparison is a Boolean operation that produces a true or a false result, depending on the values on which the comparison is performed. A comparison is performed between two values of the same type; for example, you can compare two numbers, two characters, or the names of two cities. To support comparisons, Transact-SQL provides all necessary operators.

How to create 3 tier application using LINQ

As you know that in 3 tier architecture there are three layers

User interface layer. (Is our Form in Windows application and .aspx page in Web application)
Data Access layer. (Which provides interface between Business logic layer and Database)
Business Logic layer.(Which stores your application logic)
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend