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

发布于 2024-11-07 12:42:48 字数 333 浏览 0 评论 0原文

我有一个关于 LINQ 中的查询的问题:

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

我是否正确地相信数据库将使用 Orders 数据库,并且在“ShipCity”中任何条目=“A”它将返回该 ShipCity 条目的船名?

还可以做通配符吗?例如A*

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? e.g. A*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万人眼中万个我 2024-11-14 12:42:49

是的,你是对的,这将返回所有 ShipCity 等于 "A" 的 ShipNames。可以使用 .Contains().StartsWith().EndsWith() 完成某种通配符搜索

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

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;
不必你懂 2024-11-14 12:42:49

它将使用订单,而不是数据库。数据库在逻辑上等同于 LINQ to SQL 中的上下文。否则你的假设是正确的。

要使用通配符,请使用 StartsWith() 方法。

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;
完美的未来在梦里 2024-11-14 12:42:49

您可以在此解决方案中使用 .Contains()、.StartsWith() 和 .EndsWith()

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

或者

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文