包含Statement的逗号分隔值Entity Framework

时间:2012-12-14 11:36:18

标签: linq entity-framework

我有一个逗号分隔值的LINQ语句。 我想看看我的Field是否匹配任何逗号分隔值。

 public string IdentifyProductSKU(string Serial)
    {
        int Len = Serial.Length;

        var Split = from ModelSplitter in entities.Models
                     select ModelSplitter.m_validationMask.Split(',');

        var Product = (from ModelI in entities.Models
                       where ModelI.m_validation == 0 &&
                             ModelI.m_validationLength == Len &&
                             ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
                       select ModelI.m_name).SingleOrDefault();

        return Product;
    }

解释代码:每个模型都有多个识别属性,例如。 XX1,XX5,XX7是完全相同的产品。现在,当我传入序列号时,我想根据验证掩码识别产品。例如:XX511122441141是ProductA,YY123414124是ProductC。我只是想在这一行中拆分这个查询:

ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))

我想拆分验证掩码查看序列是否包含任何验证掩码字符。这有意义吗?

2 个答案:

答案 0 :(得分:2)

这是将值拆分为列表的方法

 var split = context.Packs.Select(u => u.m_validationMask).ToList();
            List<String[]> list=new List<String[]>();
            foreach (var name in split)
            {
                String[] str = name.Split(',');
                list.Add(str);
            }

现在我需要知道如何在最终EF查询中使用该列表:

int Len = Serial.Length;

        var split = entities.Models.Select(u => u.m_validationMask).ToList();
        List<String[]> list = new List<String[]>();
        foreach (var name in split)
        {
            String[] str = name.Split(',');
            list.Add(str);
        }



        var Product = (from ModelI in entities.Models
                       where ModelI.m_validation == 0 &&
                             ModelI.m_validationLength == Len &&
                             list.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
                       select ModelI.m_name).SingleOrDefault();

        return Product;

答案 1 :(得分:0)

我不完全明白你的意思或你想要做什么。但...

如果你的ModelSplitter.m_malicationMask可以按照你所展示的那样进行拆分,那么Split就是一个List。我不明白的是,如果你想要匹配整个产品A,或者只是前三个字符,你可以修改你的查询

var Product = (from ModelI in entities.Models
               where ModelI.m_validation == 0 &&
                         ModelI.m_validationLength == Len &&
                         ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))

               let productId = ModelI.m_name.Substring(0, 3)
               where split.Contains(productId)
               select ModelI.m_name).SingleOrDefault();

如果产品不匹配,则该产品现在应为null;如果产品不匹配,则该产品应该是实际产品。