多个相似的 linq 查询

发布于 2025-01-08 16:19:59 字数 594 浏览 0 评论 0原文

我有一个模型课程,其中有多个多对多关系,例如年龄或时间。

我有这个查询:

string IDs = "1,2,3"
string[] IDList = IDs.Split(',');

return (from x in entities.Course
       where x.Ages.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
       select x);

并且我需要为时间和其他几个属性设置相同的查询,如:

string IDs = "1,2,3"
string[] IDList = IDs.Split(',');

return (from x in entities.Course
       where x.Times.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
       select x);

如何使查询更加动态,这样我就没有多个类似的查询?

谢谢

I have a model Course, which has several many to many relationships like Age or Time.

I have this query:

string IDs = "1,2,3"
string[] IDList = IDs.Split(',');

return (from x in entities.Course
       where x.Ages.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
       select x);

And I need to set the same query for Time and several other properties as in:

string IDs = "1,2,3"
string[] IDList = IDs.Split(',');

return (from x in entities.Course
       where x.Times.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
       select x);

How can I make the query more dynamic so I don't have multiple similar queries?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑咖 2025-01-15 16:19:59

您可以创建一个接受表达式(取决于您的数据类型)的方法,并以这种方式运行查询。您需要让您的年龄、时间等实现特定的接口才能正常工作。

例如,假设您使用 EF 并且您的模型是使用 DbSet 的 Code First,您可以这样做:

public interface IObject
{
    int ID { get; set; }
}

public class Age : IObject
{
    public int ID { get; set; }

    // The rest of the data
}

public class Time : IObject
{
    public int ID { get; set; }

    // The rest of the data
}

public class Course
{
    public virtual ICollection<Age> Ages { get; set; }
    public virtual ICollection<Time> Times { get; set; }
}

public class CourseContext : DbContext
{
    public DbSet<Course> Course { get; set; }
}

public class Test
{
    public IQueryable<Course> GetCourses(Expression<Func<Course, ICollection<IObject>>> exp)
    {
        var entities = new CourseContext(); 
        string IDs = "1,2,3";
        string[] IDList = IDs.Split(',');

        var c = exp.Compile();

        return entities.Course.Where(x => c.Invoke(x).Count(val => IDList.Contains(val.ID.ToString())) == IDList.Count());
    }

    public void TestMethod()
    {
        var times = GetCourses(c => (ICollection<IObject>)c.Times);
        var ages = GetCourses(c => (ICollection<IObject>)c.Ages);
    }
}

You could make a method that accepts an Expression (depeneding on your data type) and run the query that way. You'll need to make your Ages, Time, etc implement a specific interface for it to work.

For example, assuming that you are using EF and your model is Code First using DbSets, you could make this:

public interface IObject
{
    int ID { get; set; }
}

public class Age : IObject
{
    public int ID { get; set; }

    // The rest of the data
}

public class Time : IObject
{
    public int ID { get; set; }

    // The rest of the data
}

public class Course
{
    public virtual ICollection<Age> Ages { get; set; }
    public virtual ICollection<Time> Times { get; set; }
}

public class CourseContext : DbContext
{
    public DbSet<Course> Course { get; set; }
}

public class Test
{
    public IQueryable<Course> GetCourses(Expression<Func<Course, ICollection<IObject>>> exp)
    {
        var entities = new CourseContext(); 
        string IDs = "1,2,3";
        string[] IDList = IDs.Split(',');

        var c = exp.Compile();

        return entities.Course.Where(x => c.Invoke(x).Count(val => IDList.Contains(val.ID.ToString())) == IDList.Count());
    }

    public void TestMethod()
    {
        var times = GetCourses(c => (ICollection<IObject>)c.Times);
        var ages = GetCourses(c => (ICollection<IObject>)c.Ages);
    }
}
如此安好 2025-01-15 16:19:59

我会创建返回不同查询结果的方法:

public IQuerable<Course> GetAllCourses() {
    return entities.Course;
}

public IQueryable<Course> ByAge(IQueryable<Course> source, IEnumerable<String> ages {
    return from x in source
           where x.Ages.Where(val => ages.Contains(val.ID.ToString())).Count() == IDList.Count()
           select x;
}

public IQuerable<Course> ByTimes(IQueryable<Course> source, IEnumerable<String> times) {
    return from x in source
           where x.Ages.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
           select x;
}

原因是方法封装您的查询逻辑 - 只有 where 子句不同。然后您可以传入任何源。您甚至可以将两个查询过滤器组合为链式方法调用:

var ids = new [] { "1", "2", "3" };
var coursesByAgeAndTime = ByTime(ByAge(GetAllCourses(), ids), ids);

I'd make methods that return the different query results:

public IQuerable<Course> GetAllCourses() {
    return entities.Course;
}

public IQueryable<Course> ByAge(IQueryable<Course> source, IEnumerable<String> ages {
    return from x in source
           where x.Ages.Where(val => ages.Contains(val.ID.ToString())).Count() == IDList.Count()
           select x;
}

public IQuerable<Course> ByTimes(IQueryable<Course> source, IEnumerable<String> times) {
    return from x in source
           where x.Ages.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
           select x;
}

The reason is that the method encapsulates your query logic - only the where clause is different. You can then pass in any source. You could even combine the two query filters as a chained method call:

var ids = new [] { "1", "2", "3" };
var coursesByAgeAndTime = ByTime(ByAge(GetAllCourses(), ids), ids);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文