Search code examples
c#.netasp.netlinq-to-sqllinqdatasource

Where parameters in LinqDataSource


I have 2 tables:

carType:
==
id
CarTypeTitle

and

Items
==
id
ItemTitle
CarTypeId1
CarTypeId2
CarTypeId3

as you can see, i made relationship between CarTypeId (from Items table) to id (CarType Table), and relatshionship between CarTypeId2 (from Items table) to id (CarType table)... and so on.

i have 2 dropdownlist, ddl1, ddl2. ddl1 for carTypes, and ddl2 for items. i succeed with the cascading and that's work great with WHERE parameter at ddl2:

<asp:LinqDataSource ID="LinqDataSource4" runat="server" ContextTypeName="DataClassesDataContext" EntityTypeName="" TableName="Items" Where="CarTypeId1 == @CarTypeId1">
    <WhereParameters>
        <asp:ControlParameter ControlID="ddl2" Name="CarTypeId" PropertyName="SelectedValue" Type="Int32" />
    </WhereParameters>
</asp:LinqDataSource>

when i choose cartype the ddl2 give me In accordance the item for cartypeid1. but how i write the code (WHERE parameter) that will give me the type2 and 3.

i have some items that related to the same cartype, i.e: Wiper-bla is related to subaro and to fiat... so i give them the cartypeid of subaro and fiat...

hope that's all is clear.


Solution

  • Is this what you are looking for?

    <asp:LinqDataSource ID="LinqDataSource4" runat="server" ContextTypeName="DataClassesDataContext" EntityTypeName="" TableName="Items" Where="CarTypeId1 == @CarTypeId OR CarTypeId2 == @CarTypeId OR CarTypeId3 == @CarTypeId">
        <WhereParameters>
            <asp:ControlParameter ControlID="ddl2" Name="CarTypeId" PropertyName="SelectedValue" Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>
    

    However I would strongly suggest updating your data model to use a separate table to connect CarTypes and Items. e.g.

    ItemToCarType
    =============
    ItemId
    CarTypeId
    

    With this table you can include an unlimited number of CarTypes for each item. And you will simplify your queries, because you don't need to handle every field separatly (like using OR in this example)