mosyflasher
mosyflasher

Reputation: 499

Select columns with specific condition using Linq to SQL

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 ?

Upvotes: 0

Views: 5569

Answers (2)

Kapol
Kapol

Reputation: 6463

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
    };

Upvotes: 3

Ann L.
Ann L.

Reputation: 13975

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  
     };

Upvotes: 1

Related Questions