You must Sign In to post a response.
  • Category: LINQ

    How to Use Where clause in Linq ?

    Are you looking for a way to Use Where clause in Linq ? then read this thread to know more about it

    hi all,

    i written method to retrieve route stored in an database. route has sub entity called route holds two rows one for arrival and another row for departure country.i want to get routes on given arrival and departure country Id's
    .code i written as follows,

    public IList<EuropeBus.Model.Domain.Route> ViewRouteOnArrCunAndDesCun(int arrcuntry, int descountry)
    IList<EuropeBus.Model.Domain.Route> routeList = new List<EuropeBus.Model.Domain.Route>();

    using (EuropeBus.Model.Domain.EuropeBusDataContext data = new EuropeBus.Model.Domain.EuropeBusDataContext())

    System.Data.Linq.DataLoadOptions loadOptions = new System.Data.Linq.DataLoadOptions();
    loadOptions.LoadWith<EuropeBus.Model.Domain.Route>(o => o.Route_Countries);
    loadOptions.LoadWith<EuropeBus.Model.Domain.Route_Country>(or => or.MDM_City);
    loadOptions.LoadWith<EuropeBus.Model.Domain.MDM_City>(oc => oc.MDM_Country);
    loadOptions.LoadWith<EuropeBus.Model.Domain.Route>(o => o.Route_Days);
    data.LoadOptions = loadOptions;

    var r = from ru in data.Routes orderby ru.RouteCode select ru;
    if (r != null)
    foreach (EuropeBus.Model.Domain.Route route in r)

    return routeList;

    the problem is how and where to put where clause.

  • #744062
    You can add the where clause as

    Var listname =
    EuropeBus.Model.Domain.Routes.Where(s => Columnname.Contains("MyName"));

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #744378
    You can put

    var r = from ru in data.Routes
    where ru.RouteCode==1
    orderby ru.RouteCode select ru;

    Thanks & Regards
    Umesh Bhosale

  • #745536
    Hi chaminda ,

    var r = from ru in data.Routes orderby ru.RouteCode where ru.Columnname.Equql(Mact_Contation in string) select ru;

    return all column in table

    // second but both same..
    var result = from domain in EuropeBus.Model.Domain.Routes Where domain.Columnname.Contains("MyName"));
    select domain
    return all column in table

    good luck

    Surendra sharma

  • Sign In to post your comments