LINQ to SQL 查询,其中字符串以通用列表中的元素开头

发布于 2024-10-16 18:10:13 字数 974 浏览 7 评论 0原文

由于搜索要求发生了变化,我希望更新我的查询之一。最初,用户要输入单个 SKU 和制造商。搜索产品目录的日期范围。所以这就是我用的。

DateTime startDate = ...;
DateTime endDate = ...;
string prodSKU = TextSKU.Text.Trim();

var results = from c in db.Products
                where c.is_disabled == false 
                && c.dom >= startDate 
                && c.dom <= endDate 
                && c.sku.StartsWith(prodSKU)
                select c;

现在的要求是,用户可以在文本框中输入逗号分隔的 SKU 列表进行搜索。我困惑的是如何找到 mfg 中的所有产品。以 skuList 中的任何 SKU 开头的日期范围(不使用 fornext 循环)。

string prodSKU = TextSKU.Text.Trim();
List<string> skuList = prodSKU.Split(new char[] { ', ' }).ToList();

var results = from c in db.Products
                where c.is_disabled == false 
                && c.dom >= startDate 
                && c.dom <= endDate 
                // && c.sku.StartsWith(prodSKU)
                select c;

任何想法将不胜感激!

I'm looking to update one of my queries as the requirements for the search has changed. Originally, the user was to enter a single SKU and a mfg. date range to search the product catalog. So this is what I used.

DateTime startDate = ...;
DateTime endDate = ...;
string prodSKU = TextSKU.Text.Trim();

var results = from c in db.Products
                where c.is_disabled == false 
                && c.dom >= startDate 
                && c.dom <= endDate 
                && c.sku.StartsWith(prodSKU)
                select c;

Now the requirement says that the user can enter a comma delimted list of SKUs into the textbox to search. What I'm stumped about is how to find all the products in the mfg. date range that begin with any of the SKUs in skuList (w/o using a fornext loop).

string prodSKU = TextSKU.Text.Trim();
List<string> skuList = prodSKU.Split(new char[] { ', ' }).ToList();

var results = from c in db.Products
                where c.is_disabled == false 
                && c.dom >= startDate 
                && c.dom <= endDate 
                // && c.sku.StartsWith(prodSKU)
                select c;

Any ideas would be greatly appreciated!

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

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

发布评论

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

评论(2

涫野音 2024-10-23 18:10:13

像这样的东西

string prodSKU = TextSKU.Text.Trim(); 
List<string> skuList = prodSKU.Split(new char[] { ', ' }).ToList();  

var results = from c in db.Products                 
   where c.is_disabled ==false                  
   && c.dom >= startDate                  
   && c.dom <= endDate                  
   &&  skuList.Any(sl=>c.sku.StartsWith(sl))                 
      select c; 

Something like

string prodSKU = TextSKU.Text.Trim(); 
List<string> skuList = prodSKU.Split(new char[] { ', ' }).ToList();  

var results = from c in db.Products                 
   where c.is_disabled ==false                  
   && c.dom >= startDate                  
   && c.dom <= endDate                  
   &&  skuList.Any(sl=>c.sku.StartsWith(sl))                 
      select c; 
一杆小烟枪 2024-10-23 18:10:13
string prodSKU = TextSKU.Text.Trim();
List<string> skuList = prodSKU.Split(new char[] { ', ' }).ToList();

var results = from c in db.Products
                where c.is_disabled == false 
                && c.dom >= startDate 
                && c.dom <= endDate 
                &&  skuList.Contains(c.sku)
                select c;

编辑:即使这个问题已经得到解答,我也会把它放在这里。

我实现了与接受的解决方案相同的效果,但使用扩展方法来提高可读性:

public static class StringExtensions
{
    public static bool StartsWithAny(this string s, IEnumerable<string> items)
    {
        return items.Any(i => s.StartsWith(i));
    }
}

然后解决方案是:

var results = from c in db.Products
            where c.is_disabled == false 
            && c.dom >= startDate 
            && c.dom <= endDate 
            && c.sku.StartsWithAny(skuList)
            select c;
string prodSKU = TextSKU.Text.Trim();
List<string> skuList = prodSKU.Split(new char[] { ', ' }).ToList();

var results = from c in db.Products
                where c.is_disabled == false 
                && c.dom >= startDate 
                && c.dom <= endDate 
                &&  skuList.Contains(c.sku)
                select c;

EDIT: I'll put this here even though this question is already answered.

I achieved the same as the accepted solution, but using an extension method to aid readability:

public static class StringExtensions
{
    public static bool StartsWithAny(this string s, IEnumerable<string> items)
    {
        return items.Any(i => s.StartsWith(i));
    }
}

and then the solution is:

var results = from c in db.Products
            where c.is_disabled == false 
            && c.dom >= startDate 
            && c.dom <= endDate 
            && c.sku.StartsWithAny(skuList)
            select c;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文