Can Linq-To-Sql know about the similarities of the two tables?

Imagine that I have two tables in my database with the same schema (columns, types, etc.). Can Linq-To-Sql treat them as Table<AClass>using the same class for both of them? How to do it?

+3
source share
5 answers

One option is to create a view on the server that combines the results if it was just for the query.

For the upgrade scenario, you are stuck more, because LINQ to SQL will not be able to find out which table to use ...

+1
source

UNION CONCAT , . UNION CONCAT: http://blog.benhall.me.uk/2007/08/linq-to-sql-difference-between-concat.html

, , -, , ( ).

LINQPAD, , . ( , , ):

var set1 = from  t in TestTriggers
select new {t.TestTriggerID} ;

var set2 = from t in TestTrigger2s
select new {t.TestTriggerID} ;

set1.Dump();
set2.Dump();

set1.Union(set2).Dump();
set1.Concat(set2).Dump();

, . :

IOrderedQueryable<> (1 item)  
TestTriggerID 
1


IOrderedQueryable<> (2 items)  
TestTriggerID 
1

2


IOrderedQueryable<> (2 items)  
TestTriggerID 
1

2


IOrderedQueryable<> (3 items)  
TestTriggerID 
1

1

2

, CONCAT 1 , UNION .

+2

, , Table DataContext .

, , .

+2

Linq-To-Sql , ?

. DBML ( ). , LINQ-to-SQL , ?

: , , ?

+1

: . , LinqToSql .

from c in dc.Customer
where c.Address.StreetName.StartsWith("AB")
select new Person() {Name = c.Name};

A (, ) :

  • sql
  • DataContext.ExecuteQuery<Person>() sql documetation
    • sql datareader.
    • datareader DataContext.Translate<Person>() docmentation.

Customer.Name, . : datareader, DataContext.Translate<T> .

, , DataContext:

public IQueryable<CustomType> CustomTypeTable
{
  get
  {
    var query = this.Type1Table.Select(t1 => new CustomType{f1 = t1.f1 ... })
      .Concat( this.Type2Table.Select(t2 => new CustomType{f1 => t2.f1 ... })
    return query
  }
}

... ObjectTracking DataContext - PrimaryKey. () . , 1 2 .

0

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


All Articles