简体   繁体   中英

LINQ to SQL - FROM X WHERE X = “1” SELECT Y

I have a question regarding a query in LINQ:

    DataClasses1DataContext db = new DataClasses1DataContext();
    var shpnme = from p in db.Orders
                  where p.ShipCity == "A"
                  select p.ShipName;

Am I correct in believing that the database will use the Orders database, and where in "ShipCity" any entries = "A" it will return the Shipname of that ShipCity's entry?

Also can you do a wildcard? eg A*

Yes you are correct, this will return all ShipNames where the ShipCity equals "A" . A sort-of wildcard search can be done using .Contains() , .StartsWith() and .EndsWith()

var shpnme = from p in db.Orders
             where p.ShipCity.StartsWith("A")
             select p.ShipName;

It will use the Orders table , not database . The database is logically equal to the context in LINQ to SQL. Otherwise your assumptions are correct.

To do a wildcard, use the StartsWith() method.

var shpnme = from p in db.Orders
             where p.ShipCity.StartsWith("A")
             select p.ShipName;

you can do with .Contains(), .StartsWith() and .EndsWith() in this solution

DataClasses1DataContext db = new DataClasses1DataContext();
    var shpnme = from p in db.Orders
                  where p.ShipCity.Contains("A")
                  select p.ShipName;

OR

DataClasses1DataContext db = new DataClasses1DataContext();
        var shpnme = from p in db.Orders
                      where p.ShipCity.EndsWith("A")
                      select p.ShipName;

OR

DataClasses1DataContext db = new DataClasses1DataContext();
        var shpnme = from p in db.Orders
                      where p.ShipCity.StartsWith("A")
                      select p.ShipName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM