Linq grouping on view table - linq

I have a view in SQL server that is an aggregation of a many to many. So the data returned is something like this:
User1 : Data1
User1 : Data2
User1 : Data3
User2 : Data1
User2 : Data2
etc.
Normally if I'm using LINQ with Entity Framework I use the navigation properties and get nested lists. So I can loop through Users and a nested loop through the data from the join table.
With a flat view, it is just one big list. I'm trying to group on the user so I can use the same foreach loops to display data.
E.g.,
User1 - Data1, Data2, Data3
User2 - Data1, Data2, Data3
Is there a way to create the grouping this way based on a single table (view)
Hope this makes sense.

Something like this should do it.
var q = from x in db.tbl
group x by x.User into g
select new
{
User = g.Key,
Data = g.Select(y => y.Data)
};

Related

How to validate a list using PIG script

I have List 1 with following schema
{customerId: int,storeId: int,products: {(prodId: int,name: chararray)}}
Customer List with following schema
{uniqueId: int,customerId: int,name: chararray}
Store List with following schema
{uniqueId: int,storeNum: int,name: chararray}
and Product List with schema
{uniqueId: int,sku: int,productName: chararray}
Now I want to search customerId , storeId and prodId of each item in List 1 with other lists to check the ids are valid or not. The valid items has to be stored in on file and invalid items in another.
As PIG is very new for me, I feel this as very complex to do. Please give me a good logic to do this job using Apache PIG.
First of all load all your data think these as tables
cust_data = LOAD '\your\path\to\customer\data' USING PigStorage() as (uniqueId: int, customerId: int, name: chararray);
store_data = LOAD '\your\path\to\store\data' USING PigStorage() as (uniqueId: int, storeNum: int, name: chararray);
product_data = LOAD '\your\path\to\product\data' USING PigStorage() as (uniqueId: int, sku: int, productName: chararray);
You can check your loaded data schema by
DESCRIBE cust_data;
DESCRIBE store_data;
DESCRIBE product_data;
JOIN Customer and Store data first using uniqueId (we are doing a equijoin)
cust_store_join = JOIN cust_data BY uniqueId, store_data BY uniqueId;
then Generate your columns
cust_store = FOREACH cust_store_join GENERATE cust_data::uniqueId as uniqueId, cust_data::customerId as customerId, cust_data::name as cust_name, store_data::storeNum as storeNum, store_data::name as store_name;
Now JOIN customer store and product using uniqueId (we are doing a equijoin)
cust_store_product_join = JOIN cust_store BY uniqueId, product_data BY uniqueId;
finally generate all your desired columns
customer_store_product = FOREACH cust_store_product_join GENERATE cust_store::uniqueId as uniqueId, cust_store::customerId as customerId, cust_store::cust_name as cust_name, cust_store::storeNum as storeNum, product_data::sku as sku, product_data::productName as productName;
now store your desired columns in your local /hdfs directory
below store command will store all the matching uniqueId from all three tables i.e., customer, store, product
STORE customer_store_product INTO '\your\output\path' USING PigStorage(',');
Similarly you can join your list1 schema and generate columns and store data using same logic.
Hope this helps

EF6 How to query where children contains all values of a list

Say I have a document table, with doc_id (PK) and doc_name fields, a category table with cat_id (PK) and cat_name fields, and a document_categories table with doc_id (PK, FK) and cat_id (PK, FK) fields, so I can attribute one or many categories to each document.
I have generated a model with EF6 in "Database first" mode, which gives me two entities: document and category, each containing a field which is a collection of children.
document contains a categories field which lists the categories of the document, and vice-versa in the category entity.
Now, I want to query all documents that contain category 1 AND category 2.
Let's say the database contains the following documents:
Doc A: Categories 1, 3
Doc B: Categories 1, 2
Doc C: Categories 1
Doc D: Categories 1, 2, 3
My query should return docs B and D.
How can I achieve that with EF6 using Linq?
Searched long on this site and in Google but found nothing for this particular request ... Thanks for your help
Use this:
var ids = new int[]{1,2};
var docs = context.Documents
.Where(d=> ids.All(cid=> d.Categories.Any(dc=>dc.cat_id == cid))).ToList();
Or
var ids = new int[]{1,2};
var result = from d in context.Documents
where ids.All(id => d.Categories.Any(dc=> dc.cat_id == id))
select s;

Linq2Entity Getting Values from reference Table

i have 3 tables
Table: A
- aID
- Text
Table: B
- bID
- Text
Table: A_B (reference table which holds both of the primary keys as foreign keys)
- aID
- bID
The Entity Framework knows that Table A_B is just a reference table and does not create it in the DBModel but keeps the references in Table A and B.
My question is how do i get the data which is in the reference table without actually having this table? When i try to access the values of Table B using the reference which is in Table A i cant get the values of the Table B Colums.
ObjectQuery<A> aTable = dbConnection.A;
var result = from data in aTable
where data.aID = '12'
select data.B; //B is the reference to table B out of table A
What i need is something like:
ObjectQuery<A> aTable = dbConnection.A;
var result = from data in aTable
where data.aID = '12'
select data.B.bID;
How do i get data out of Table B using the reference between Table A and Table B?
Using: VS 2010 Prof, .Net 4, Linq2Entity
As Gary Said, you either use First (if you want one B for each A), FirstOrDefault (if you want nulls where there is no B for A) or SelectMany (if you want all Bs for each A)
The latter can be written like this:
var result = from data in dbConnection.A
from b in data.B
select b.Id;

LINQ Joining 2 tables

I have two tables in the database one contains a list of all possible grocery values. For Example
Milk
Cheese
Bread
Meat
the second table contains Items from Grocery that are selected. For Example:
Milk
Cheese
I want a result that has all possible grocery items with Milk and Cheese selected.
Any ideas?
Here are the tables.
The GroceryList Table:
ID INT PK
Description Varchar(50)
The ShoppingList Table:
ID INT PK
GroceryListID int FK to GroceryList.ID
So the resulting Entity would be all items from GroceryList and if they exist in ShoppingList then selected is marked as true:
ShoppingList.ID
Grocerylists.Description
Selected
Based on understanding you can do something like this
//first get the list of product which satisfy your condition
var ids = (from p ShoppingList
select p.GroceryListID ).ToList();
//second filter the Grocery products by using contains
var myProducts = from p in GroceryList
where ids.Contains(p.ID)
Select p;
or
if you want to get info about join than this image would help you
Inner Join
Outer Join
Try to understand and which may help you to resolve your query
Edit: Still sounds like you want to do a left join. In your case:
var LINQResult = from g in Datacontext.GroceryList
from s in DataContext.ShoppingList
.Where(c=>c.ID == g.ID)
.DefaultIfEmpty()
select new {
g.ID,
g.Description,
s.ID // Will be null if not selected.
};
For more examples:
Left Join on multiple tables in Linq to SQL

Linq query 'Id in query' how to?

Is there any tweek to use IN like query in lambda?
for example i have a query
Select * from Users where Id in ( 1,45,67, 89)
can i write the same in linq?
for example i have list of user say
List<Users> oUserList= new List<Users>();
and i have int list
List<Int32> Ids
and i want to write query like
var data= select all users from 'oUserList' where id not in 'Ids'
can any body tell me how to write this?
thanks
Issue solved
var data = oUserInfolist.Where(x => (!oo.Contains(x.ID)));
from u in oUserList where !Ids.Contains(u.UserID) select u

Resources