相关疑难解决方法(0)

带有 Or 的 IQueryable Where 扩展方法

复制:

如何将 OR 运算符动态添加到 LINQ 中的 WHERE 子句

我想遍历一个字符串值数组并构建一个 linq 表达式

列表中的每个项目都被 OR 运算在一起。

string[] search = new string[]{"A", "B", "C"};
foreach (string item in filterValues)
{
    searchQuery = searchQuery.Where(s => s.Name.Contains(item));
}
Run Code Online (Sandbox Code Playgroud)

上面的代码搜索“A”“B”“C”

我想搜索“A”“B”“C”。

我知道如何用 Linq 做到这一点,但我想使用扩展方法来完成同样的事情。

c# linq iqueryable

5
推荐指数
1
解决办法
6192
查看次数

使用 AND 和 OR 的 C# 谓词构建器

我有以下课程:

public class testClass
{
    public string name { get; set; }
    public int id { get; set; }
    public int age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

以及以下代码:

            var list = new List<testClass>();
            list.Add(new testClass { name = "name", id = 1, age = 30 });
            list.Add(new testClass { name = "name", id = 2, age = 22 });
            list.Add(new testClass { name = "name", id = 3, age = 20 });
            list.Add(new testClass { name = "name", id = …
Run Code Online (Sandbox Code Playgroud)

c# linq predicatebuilder

5
推荐指数
1
解决办法
3610
查看次数

LinqToSql奇怪的行为

我有以下代码:


var tagToPosts = (from t2p in dataContext.TagToPosts
                                    join t in dataContext.Tags on t2p.TagId equals t.Id
                                    select new { t2p.Id, t.Name });
//IQueryable tag2postsToDelete;
foreach (Tag tag in tags)
{
    Debug.WriteLine(tag);
    tagToPosts = tagToPosts.Where(t => t.Name != tag.Name);
}
IQueryable tagToPostsToDelete = (from t2p in dataContext.TagToPosts
                                            join del in tagToPosts on t2p.Id equals del.Id
                                            select t2p);
dataContext.TagToPosts.DeleteAllOnSubmit(tagToPostsToDelete);
Run Code Online (Sandbox Code Playgroud)

where tagsList<Tag>- 构造函数创建的标签列表,因此它们没有id.我运行代码将一个刹车点放在Debug上,然后等待几个周期.然后我将WatchToPosts.ToList()放在Watch窗口中以便执行查询.在SQL事件探查器中,我可以看到以下查询:

exec sp_executesql N'SELECT [t0].[Id], [t1].[Name]
FROM [dbo].[tblTagToPost] AS [t0]
INNER JOIN [dbo].[tblTags] AS [t1] ON [t0].[TagId] = …
Run Code Online (Sandbox Code Playgroud)

c# linq lambda linq-to-sql

3
推荐指数
1
解决办法
3062
查看次数

标签 统计

c# ×3

linq ×3

iqueryable ×1

lambda ×1

linq-to-sql ×1

predicatebuilder ×1