【发布时间】:2010-04-11 10:47:32
【问题描述】:
我正在尝试使用 Linq2Sql 返回包含字符串列表中的值的所有行。 linq2sql 类对象有一个字符串属性,其中包含用空格分隔的单词。
public class MyObject
{
public string MyProperty { get; set; }
}
MyProperty 值示例如下:
MyObject1.MyProperty = "text1 text2 text3 text4"
MyObject2.MyProperty = "text2"
例如,使用字符串集合,我通过下面的列表
var list = new List<>() { "text2", "text4" }
这将返回我上面示例中的两个项目,因为它们都包含“text2”值。
我尝试使用以下代码进行以下操作,但由于我的扩展方法,无法评估 Linq2Sql。
public static IQueryable<MyObject> WithProperty(this IQueryable<MyProperty> qry,
IList<string> p)
{
return from t in qry
where t.MyProperty.Contains(p, ' ')
select t;
}
我还写了一个扩展方法
public static bool Contains(this string str, IList<string> list, char seperator)
{
if (str == null) return false;
if (list == null) return true;
var splitStr = str.Split(new char[] { seperator },
StringSplitOptions.RemoveEmptyEntries);
bool retval = false;
int matches = 0;
foreach (string s in splitStr)
{
foreach (string l in list)
{
if (String.Compare(s, l, true) == 0)
{
retval = true;
matches++;
}
}
}
return retval && (splitStr.Length > 0) && (list.Count == matches);
}
关于如何实现这一目标的任何帮助或想法?
【问题讨论】:
标签: c# linq-to-sql list comparison