Question

i'm working on this example: http://code.msdn.microsoft.com/wpapps/Beginners-for-how-to-use-45690caa

i have inserted a new column in the table, called "category"

now i need to view in the listbox only the rows with category == "card"

this is the listbox:

<ListBox x:Name="ContactListBox" Grid.Row="2">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="100" />
                                        <ColumnDefinition Width="200" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Image Source="Images\open.png" Margin="-30, -18, 0, 0" Tap="Edit_Tap" Height="75" Width="75"/>
                                    <TextBlock Text="{Binding Name}" Tap="Edit_Tap" Grid.Column="1" FontSize="{StaticResource PhoneFontSizeLarge}" TextAlignment="Left"/>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

and this is the source of the listbox:

void Customers_Loaded(object sender, RoutedEventArgs e)
    {
        using (AddressBookDataContext dc = new AddressBookDataContext())
        {

            ContactListBox.ItemsSource = from c in dc.Contacts select c;

        }
    }

i've tried to change it in: ContactListBox.ItemsSource = from c in dc.Contacts where c.category == "card" select c; but i doesn't work, what can i do?

thanks everyone and sorry for my bad english

Was it helpful?

Solution

You didn't mention which exception you got, but it sounds like you're probably getting `NotSupportedException: the member "[whatever]" has no supported translation to SQL error".

If you search for this exception you'll find a lot of answers along a common theme - essentially Linq2SQL doesn't know how to translate "Type" into SQL so it falls over. In your case you can fix this by using Linq2SQL and Linq2Object:

// L2S from the DB, transferred to an Enumerable
var itemSource = (from c in dc.Contacts select c).AsEnumerable();

ContactListBox.ItemsSource = itemSource.Where(c => c.Type == "q");

// or full form Linq
ContactListBox.ItemsSource = from c in itemSource where c.Type == "q" select c;

In my case I didn't add a column, I just added multiple contacts with different Types and filtered only the ones of Type "q".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top