使用"匹配"在一个LINQ声明 [英] Using "Match" in a Linq statement

查看:100
本文介绍了使用"匹配"在一个LINQ声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个记录的表(会有很多在运行时)。记录的DEVICEID是DEVICE1和DEVICE2。我想用正则表达式来提取记录。下面的代码编译,但无法返回结果。当我将鼠标悬停在光标devices.ToList()语句我碰到下面的错误,基地{System.SystemException} = {LINQ到实体无​​法识别方法System.Text.RegularExpressions.MatchCollection匹配(系统.String)'方法,和这种方法不能被翻译成商店表达。}。谁能告诉我我该怎么修改我的查询,使这将基于表达式返回记录?



  filterText = @DEVICE ; 
正则表达式搜索关键词=新的正则表达式(filterText);

使用(VAR CTX =新MyEntities())
{
无功装置从D =在ctx.Devices
让比赛= searchTerm.Matches(d.DeviceId )
,其中matches.Count> 0
选择((设备)D);
返回devices.ToList();
}


解决方案

我不相信你可以使用LINQ的正则表达式的实体。但是,它看起来像你只是想找到它与设备启动设备,所以查询将是:



 返回ctx.Devices.Where(D => d.DeviceId.StartsWith(设备))
.ToList();



编辑:如果你确实需要一个正则表达式的灵活性,你应该首先提取设备ID (和的设备ID)返回给客户机,然后对这些正则表达式,最后取相匹配的查询数据的其余部分:

 正则表达式的regex ​​=新的正则表达式(...); 

VAR deviceIds = ctx.Devices.Select(D =>的DeviceID).AsEnumerable();

VAR matchingIds = deviceIds.Where(ID => regex.IsMatch(ID))
.ToList();

无功设备= ctx.Devices.Where(D => matchingIds.Contains(d.DeviceId));

这是假设它实际上是获取所有设备开始与所有的数据昂贵。如果这不是太糟糕了,这将是一个简单的选择。要强制处理过程中进行,使用 AsEnumerable()

 无功设备= ctx.Devices.AsEnumerable()
。凡(D => regex.IsMatch(d.DeviceId))
.ToList();


I have a table that has two records (there will be many at runtime). The deviceId of the records are, "DEVICE1" and "DEVICE2". I want to use a regular expression to extract records. The code below compiles but fails to return a result. When I hover the cursor on the "devices.ToList()" statement I get the following error, "base {System.SystemException} = {"LINQ to Entities does not recognize the method 'System.Text.RegularExpressions.MatchCollection Matches(System.String)' method, and this method cannot be translated into a store expression."}". Can anyone show me how I can modify my query so that this would return records based on the expression?

filterText = @"DEVICE.";
Regex searchTerm = new Regex(filterText);

using (var ctx = new MyEntities())
{
 var devices = from d in ctx.Devices
                let matches = searchTerm.Matches(d.DeviceId)
               where matches.Count > 0
               select ((Device)d);
return devices.ToList();
}

解决方案

I don't believe you can use regular expressions with LINQ to Entities. However, it looks like you're just trying to find devices which start with "DEVICE", so the query would be:

return ctx.Devices.Where(d => d.DeviceId.StartsWith("DEVICE"))
                  .ToList();

EDIT: If you actually need the flexibility of a regular expression, you should probably first fetch the device IDs (and only the device IDs) back to the client, then perform the regular expression on those, and finally fetch the rest of the data which matches those queries:

Regex regex = new Regex(...);

var deviceIds = ctx.Devices.Select(d => DeviceId).AsEnumerable();

var matchingIds = deviceIds.Where(id => regex.IsMatch(id))
                           .ToList();

var devices = ctx.Devices.Where(d => matchingIds.Contains(d.DeviceId));

That's assuming it would actually be expensive to fetch all the data for all devices to start with. If that's not too bad, it would be a simpler option. To force processing to be performed in process, use AsEnumerable():

var devices = ctx.Devices.AsEnumerable()
                         .Where(d => regex.IsMatch(d.DeviceId))
                         .ToList();

这篇关于使用"匹配"在一个LINQ声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆