2012-06-06 92 views
1

让与将用于筛选结果字符串列表开始:获取SQL LINQ结果

List<String> RadioNames = new List<String>(); 
RadioNames.AddRange(new String[] { "abc", "123", "cba", "321" }); 

我希望能够基于LINQ筛选到SQL数据库表在RadioNames,但赶上是我想RadioNames是部分匹配(这意味着它将捕获Radio123,而不仅仅是123)。

,我需要过滤的源代码如下:

var ChannelGrants = from cg in sddc.ChannelGrants 
        select new 
        { 
         cg.ID, 
         cg.Timestamp, 
         cg.RadioID, 
         cg.Radio 
        }; 

所以我需要执行类似下面(外原ChannelGrants结果,因为这是一个条件搜索)

if(RadioNamesToSearch != null) 
{ 
     List<String> RadioNames = new List<String>(); 

     // Here I split all the radio names from RadioNamesToSearch based on a command separator and then populate RadioNames with the results 

     ChannelGrants = from cg in ChannelGrants 
         where ??? 
         select cg; 
} 

我需要帮助吗?在上面的代码中(或者如果ChannelGrants = ...一起无效)。重复上面,我需要过滤ChannelGrants返回从RadioNames任何匹配,但它会做部分匹配(这意味着它将捕获Radio123,而不仅仅是123)。

所有的代码被包含在作为这样的方法,...

public static DataTable getBrowseChannelGrants(int Count = 300, String StartDate = null, String StartTime = null, String EndDate = null, String EndTime = null, String RadioIDs = null, String RadioNamesToSearch = null, String TalkgroupIDs = null, String TalkgroupNames = null, bool SortAsc = false) 
+0

'RadioNames'和'RadioNamesToSearch'有什么区别?后者从哪里来?你还没有在任何地方宣布它。 –

+0

对不起,我编辑了我的帖子以显示代码所包含的方法。我省略了大部分270行代码以提高可读性。 –

+0

对此有何更新? – seekerOfKnowledge

回答

0

目前,似乎没有最好的方法,所以我会回答这个问题,直到一个新的答案不重复其他答案,而这个答案不适用于这个线程。

0

代替的???

RadioNames.Where(rn=>cg.Radio.ToLower().Contains(rn.ToLower())).Count() > 0 

应该这样做......

则tolower()的调用是可选的,当然。

编辑:我只是写了这个,它在控制台应用程序中工作正常。结果包含一个项目,WriteLine吐出“cbaKentucky”。不知道该说什么呀。

class Program 
{ 
    static void Main(string[] args) 
    { 
     List<String> RadioNames = new List<String>(); 
     RadioNames.AddRange(new String[] { "abc", "123", "cba", "321" }); 

     List<ChannelGrants> grants = new List<ChannelGrants>(); 
     grants.Add(new ChannelGrants() { ID = 1, Radio = "cbaKentucky", RadioID = 1, TimeStamp = DateTime.Now }); 

     var result = from cg in grants 
        where RadioNames.Where(rn=>cg.Radio.ToLower().Contains(rn.ToLower())).Count() > 0 
        select cg; 

     foreach (ChannelGrants s in result) 
     { 
      Console.WriteLine(s.Radio); 
     } 
    } 
} 

class ChannelGrants 
{ 
    public int ID { get; set; } 
    public DateTime TimeStamp { get; set; } 
    public int RadioID { get; set; } 
    public string Radio { get; set; } 
} 
+0

我得到的错误“System.NotSupportedException被用户代码未处理 - 消息=本地序列不能用于查询运算符的LINQ to SQL实现,除了Contains运算符。” –

+0

我的代码是使用是:'where filterRadioNames.Where(frn => cg.Radio.Name.ToLower()。Contains(frn.ToLower()))。Count()> 0' –

0

ChannelGrants中你在比较RadioNames的哪个字段?

要检索只在您的RadioNames列表中的条目,你会使用含有方法这样

ChannelGrants = from cg in ChannelGrants 
        where RadioNames.Contains(cg.Radio) 
        select cg; 

(如果你想找到那个曾在电台财产的RadioNames之一的所有行。与相应的列你匹配)

这给你一个类似的结果,如果你在SQL

where cg.Radio in ("abc", "123", "cba", "321") 

从日过这样的where子句替换cg.Radio是链接How to do SQL Like % in Linq? 它看起来像你可以用类似的匹配组合它,但添加斜线,这不是我亲自做过的事情。

+0

对不起,字段'cg.Radio.Name'与RadioNames列表相匹配 - 我现在试试这个代码 –

+0

我别无选择,只能将你所要的内容翻译成哪里RadioNames.Any(frn => cg.Radio.Name.ToLower()。Contains() frn.ToLower())),因为我正在查看是否有任何'RadioNames'包含在cg.Radio.Name属性中,我得到了错误“本地序列不能用于查询运算符的LINQ to SQL实现,除非包含运算符“。 –