add all rows of multiple tables into one and group using linq - entity-framework

Hi i am trying to create a search query with entityframework
I am doing something like this
model = _context.articles.where(combination 1)
model2 = _context.tags.where(combination 2).select(x=> new {article=..})
model3 = _context.keywords.where(combination 3).select(x=> new {article=..})
I am not generating one model as its not possible or out of my capacity to create such query but now since i have 3 models with similar columns all i want to do is merge them into one and remove duplicates.
I don't know if this is good approach but is it possible?

Use union and distinct methods as following:
var result = model.Union(model2).Union(model3).Distinct().ToList();

Related

how can combine multi table with same columns in one common view model

I have 14 tables with some same columns name and types.I want to Export them to excel.
How can I combine and Merge these common columns in one view Model in Entity Framework or Linq with One query ?
now I query 14 tables separately and convert to list and add them to common list.
var listAll = new List<requestViewExcel>();
listAll.AddRange(r1);
listAll.AddRange(r2);
listAll.AddRange(r3);
listAll.AddRange(r4);
listAll.AddRange(r5);
listAll.AddRange(r6);
listAll.AddRange(r7);
listAll.AddRange(r8);
listAll.AddRange(r9);
listAll.AddRange(r10);
listAll.AddRange(r11);
listAll.AddRange(r12);
listAll.AddRange(r13);
listAll.AddRange(r14);
I don't like this Solution. is it another better way ?
I think you can use Concat()
var listAll = r1.Concat(r2).Concat(r3).Concat(...);
You can also use Union() but it will only return the unique items in the collections

Updating specific columns not all model entities

Zend 1.8 Geeks!
I always use model_mapper to update insert and fetch data from the db by this example :
$a=somthin;
$y=qwe;
$dataMapper = new model_mapper_data();
$dataModel = new model_data();
$dataEntity=$dataModel->AA=$a ;
=> $dataMapper->update($dataEntity,'x'=$y);
Now on the last line the Mapper generates a query to update all table row columns AA and rest stored in the data-model where 'x'=$y.
isn't there a way to force the Mapper to update specific columns only?
Well all what you need is to not give the update method the model entity Since the model initialize objects for all of the columns .
Just give it an array of the columns names you would like to modify and Pair them to their values like below :
Mapper->update('column_name'=>value);

EntityFramework 6: ExecuteStoreQueryAsync with multiple result sets

I'm using EF 6 beta1. I have a simple stored proc that returns two result sets:
ALTER PROCEDURE GetItemsByParentId
#parentID int
SELECT * FROM Table1 WHERE ParentID = #parentID
SELECT * FROM Table2 WHERE ParentID = #parentID
I want to call that stored proc using my ObjectContext. I'm using database first and I have an edmx, but I don't want to import the stored proc as a function if I don't have to.
I created two simple classes Table1DTO and Table2DTO to store the results of the stored proc. For the first result set I call this:
var result1 = ExecuteStoreQueryAsync< Table1DTO >("EXEC GetItemsByParentId #parentID", new SqlParameter("parentID", parentID));
List table1DTOList = result1.ToList();
That works fine. Then for the second result set I'm trying this:
var result2 = result.GetNextResult< Table2DTO >();
List improvementDetailInfos = result2.ToList();
The problem is result2 is coming back as null. What am I missing?
This may sound like splitting hairs, but Entity Framework supports multiple result sets. The problem is that the Entity Framework Tooling does not. This has been an issue since EF 4.1/4.2. So you have three options (that I can think of):
split up your calls to only use single result sets
use an alternate technology (dapper?) for the few critical stored procedures that require multiple result sets
manually edit the EDMX to create a multi-result set mapping for GetItemsByParentId
To be honest I've wrestled with this in a number of scenarios and none of the options are great. If you're something of a masochist, I wrote up an example and answered a question on how to edit the EDMX for multiple result sets.

MVC 3 Entity Framework 4.1 get a list of unique data rows

I am using MVC 3 and Entity Framework 4.1. I need to return a view that has a list of rows of DISTINCT values from my Documents database table. In SQL Server, the query that works is as follows:
SELECT DISTINCT(DocNum), Title, DocDate, DocFileName FROM Documents
How do I do the same thing in MVC 3?
var result = (from d in cntx.Documents
select d).Distinct();
Try:
var query = context.Documents.Select(x => new
{
x.DocNum,
x.Title,
x.DocDate,
x.DocFileName
}).Distinct().ToList();
Distinct must go over all returned columns otherwise you could end up with single DocNumber and for example multiple dates and query engine wouldn't know which date to select because only one record with given DocNumber can be returned.

How to do a joined query in the ZF tables interface?

I have the db and my tables look like this:
alt text http://img15.imageshack.us/img15/2568/stackdijag.png
What I want to do is to get all models where manufacturers name column starts with A.
Which means that that simple part of query should be like $manufacturers->fetchAll("name LIKE '$letter%'");
I am trying to accomplish this with ZF relations but it ain't going, so any kind of help is welcome...
$models = new Models();
$select = $models->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false)
->join(array("a"=>"manufacturers"), 'models.manufacturer_id = a.id',
array("man_name"=>"name", "man_description"=>"description"))
->where("a.name LIKE 'A%'");
$rowset = $models->fetchAll($select);
Unfortunately the Zend_Db_Table relationships interface doesn't have much intelligence in it related to creating joined queries from its declared reference map. The community-contributed solution for complex queries is the Zend_Db_Table_Select query factory.
Note you have to give column aliases for manufacturer's name and description, or else these columns will suppress the model's name and description in the associative array for the row data. You should name columns distinctly to avoid this.
But in your case, I'd skip the table interface and the select interface, and simply execute an SQL query directly using the Db adapter:
$data = $db->fetchAll("
SELECT m.*, a.name AS man_name, a.description AS man_description
FROM Models m JOIN Manufacturers a ON m.manufacturer_id = a.id
WHERE a.name LIKE 'A%'");
You'll get the data back as a simple array of associative arrays, not as a Zend_Db_Table_Rowset. But since a joined rowset isn't writeable anyway, you haven't sacrificed much.