LinqToSql Select for class, then run additional queries

I have a LINQ query with multiple joins, and I want to pass it as IQueryable<T>well as apply additional filters in other methods.

The problem is that I can’t decide how to pass the data type varand keep it strictly typed, and if I try to put it in my own class (EG:) .Select((a,b) => new MyClass(a,b)), I get errors when I try to add later sentences Where, because my class no translations to SQL. Is there a way to do one of the following:

  • Make my SQL class map?
  • Create a type interface varfor the data interface (so that I can pass it as if it is)?
  • Something that I didn’t have, will this solve my problem?

Example:

public void Main()
{
    using (DBDataContext context = new DBDataContext())
    {
      var result = context.TableAs.Join(
         context.TableBs,
         a => a.BID,
         b => b.ID,
        (a,b) => new {A = a, B = b}
      );
      result = addNeedValue(result, 4);
   }
}

private ???? addNeedValue(???? result, int value)
{
    return result.Where(r => r.A.Value == value);
}

PS: , , , .

+3
4

, , - , - new { A = a, B = b } . IQueryable<your type>

:

public class MyClass 
{ 
    public int A { get; set; }
    public int B { get; set; }
}

public void Main()
{
    using (DBDataContext context = new DBDataContext())
    {
      var result = context.TableAs.Join(
         context.TableBs,
         a => a.BID,
         b => b.ID,
        (a,b) => new MyClass {A = a, B = b}
      );
      result = addNeedValue(result, 4);
   }
}

private IQueryable<MyClass> addNeedValue(IQueryable<MyClass> result, int value)
{
    return result.Where(r => r.A.Value == value);
}
+2

; Join, . , ( , , ).

.NET 3.5 .NET 4; , 3.5 (SP1) Expression.Invoke ( ) EF, LINQ-to-SQL.

, Northwind ( , , ):

using System;
using System.Linq;
using System.Linq.Expressions;
using ConsoleApplication1; // my data-context namespace
static class Program
{
    public static void Main()
    {
        using (var context = new TestDataContext())
        {
            context.Log = Console.Out; // to check it has worked
            IQueryable<Order> lhs = context.Orders;
            IQueryable<Order_Detail> rhs = context.Order_Details;
            // how ever many predicates etc here
            rhs = addBeforeJoin(rhs, 4);

            var result = lhs.Join(rhs,
                   a => a.OrderID,
                   b => b.OrderID,
                  (a, b) => new { A = a, B = b }
            );
            // or add after
            result = result.Where(row => row.B, addAfterJoin(100));
            Console.WriteLine(result.Count());
        }
    }

    private static IQueryable<Order_Detail> addBeforeJoin(IQueryable<Order_Detail> query, int value)
    {
        return query.Where(r => r.Quantity >= value);
    }
    private static Expression<Func<Order_Detail, bool>> addAfterJoin(int value)
    {
        return r => r.Quantity <= value;
    }
    private static IQueryable<TSource> Where<TSource, TProjection>(
        this IQueryable<TSource> source,
        Expression<Func<TSource, TProjection>> selector,
        Expression<Func<TProjection, bool>> predicate)
    {
        return source.Where(
            Expression.Lambda<Func<TSource, bool>>(
            Expression.Invoke(predicate, selector.Body),
            selector.Parameters));
    }

}
+1

, " " , - , , A B. , , , , .

, SQL/LINQ-to-SQL, , , .

LINQ to SQL , . DBML , LINQ-to-SQL . IQueryable<ResultSetRow>.

0

, , , , .NET 4.0.

0

Source: https://habr.com/ru/post/1742391/


All Articles