0

I have a select query with Linq like this:

        string _mycondition = "0";

          DataClasses1DataContext m = new DataClasses1DataContext();
           var q = from p in m.ViewPersonCarRelations.AsEnumerable()                    
                select new
                {
                    FirstName = p.name,
                    LastName = p.Lname  
                };

I want to select my columns with a condition , for example : only if _mycondition == "0" is true , select p.name and name it FirstName otherwise my variable should not has FirstName . I was wonder, is it possible to do that with Linq ?

2
  • do you mean you want to select all where firstname is not empty Commented Aug 5, 2015 at 21:42
  • @OmarQaddoumi no! i want to decide in runtime that select FirstName or not ! Commented Aug 5, 2015 at 22:01

2 Answers 2

3

No, it is not possible. The easiest way to achieve this would be to use the ternary operator like this:

var q = from p in m.ViewPersonCarRelations.AsEnumerable()                    
                select (_mycondition == "0") ?
                    new 
                    {
                        FirstName = p.name,
                        LastName = p.Lname  
                    }
                    :
                    new 
                    {
                        LastName = p.Lname
                    };

This code will probably result in the following compiler error:

Type of conditional expression cannot be determined because there is no implicit conversion between 'AnonymousType#1' and 'AnonymousType#2'

Since there is no way to overload operators for anonymous types, you will have to find another way of achieving your goal. You can create two classes, overload the implicit operator and not use anonymous types at all.

EDIT

There actually is a way to do this, but I'd strongly advice against it - you will get an Enumerable<object> as the result. Since object is the base type of all types in the .NET framework, you can cast the second anonymous class to an object (you could also cast the first one, but that would be redundant).

select (_mycondition == "0") ?
    new 
    {
        FirstName = p.name,
        LastName = p.Lname 
    }
    :
    (object)new 
    {
        LastName = p.Lname
    };
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand what you're saying, you want to decide at run time whether the anonymous type q that you're creating will have a FirstName property or not.

That isn't possible in a static type system. The reason is that, even though you don't specify the type of q, it does have a type: it's only "anonymous" to us, the coders. The compiler has to know what it is. It can't remain indeterminate until run time.

There is one exception to this, though. I believe you could do something like what you want using an ExpandoObject, part of the System.Dynamic namespace. That will let you create new properties at run time.

But it would be simpler just to have FirstName be defined on your type and sometimes have a null value.

 string _mycondition = "0";

 DataClasses1DataContext m = new DataClasses1DataContext();
 var q = from p in m.ViewPersonCarRelations.AsEnumerable()                    
     select new
     {
          FirstName = (_mycondition=="0") ? p.name : null,
          LastName = p.Lname  
     };

3 Comments

As you said , i want to decide in runtime that my variable contains FirstName or not
Because i want to export my query to excel , and my column shouldn't exist !
@mosyflasher: Maybe you need a different data structure than a class with one property = one column. Consider using an old-fashioned DataTable. You can define new columns at run time using whatever criteria you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.