views:

11

answers:

1

I have a table where I have items under two different types as you can see below: How can I select from this table with Linq to Entity to get the result with two variables?

where ItemType = Type1 and ItemType == Type2
.... select new {typeOne == "", typeTwo == ""};

ID ItemName     ItemType

1 ItemOne       Type1
2 ItemTwo       Type1
3 ItemThree     Type1
4 ItemFour      Type1
5 ItemTFive     Type2
6 ItemSix       Type2
7 ItemSeven     Type2
8 ItemEight     Type2
A: 

I'm assuming (based on your comments) that you want to find the items where the ItemType matches either one column or another, then map the correct column name to ItemType in the output. I'll show it using the fluent-style, extension methods.

 var query = db.Items
               .Where( i => i.Type1 == ItemType || i.Type2 == ItemType )
               .Select( i => new
                {
                    ID = i.ID, 
                    ItemName = i.ItemName,
                    ItemType = ItemType == i.Type1 ? "Type1" : "Type2"
                });

If I've misunderstood your intentions, please leave a comment and clarify.

tvanfosson
I would like to select type1 and type2 at the same time just using 2 variable
George