Click here to Skip to main content
15,888,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Codeproject,

How can I send parameter to an Expression method in Linq??

When I am Trying this.
C#
var y3 = from p in Dc.Table4s where funtion(new Table4(){Num = 1}) select p;

Getting result in y3.

But, when I try to pass p as parameter its raising error ' Operator '==' cannot be applied to operands of type 'bool' and 'AliasRnD.YesNo'

C#
var y4 = from p in Dc.Table4s where funtion(p) == YesNo.Yes select p;

How to pass p as a parameter and get the same result?
My code is given below.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Linq.Expressions;
using System.Collections;

namespace AliasRnD 
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window 
    {
        public MainWindow()
        {
            InitializeComponent();
            DataClasses1DataContext Dc = new DataClasses1DataContext();

               
            System.Linq.Expressions.Expression<Func<Table4, Boolean>> expression1 = IsEqual(YesNo.Yes);
            var funtion = expression1.Compile();

                      var y3 = from p in Dc.Table4s where funtion(new Table4(){Num = 1}) select p;


            var y4 = from p in Dc.Table4s where funtion(p) == YesNo.Yes select p;
             
           
              }

        Expression<Func<Table4, bool>> IsEqual(YesNo val) 
        {
            return ((p) => p.Num == (int?)val);
        }
           
        
 
   public  enum YesNo : int
    {
        Yes = 1, No = 2
    }



Regardz

Salam6331
Posted
Updated 11-Jan-12 19:17pm
v2

1 solution

Well, actually the compiler tells you already what's wrong :)

' Operator '==' cannot be applied to operands of type 'bool' and 'AliasRnD.YesNo'

And that's the line
C#
var y4 = from p in Dc.Table4s where funtion(p) == YesNo.Yes select p;


There you are comparing a bool (function(p)) with an enum.
To solve it, just drop the comparison. It would be reduntant anyway. You defined your function as IsEqual( YesNo.Yes ) above.

C#
var y4 = from p in Dc.Table4s where funtion(p) select p;


Best regards
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900