Select list value based on second column - linq

I have 2 column list value
ptext | stylename
-----------|------------
aaa | styleone
ccc | styleone
ddd | styleone
fff | styletwo
ggg | styletwo
xyx | styleone
dkk | styleone
I would like to have List like this
ptext | stylename
----------------|------------
aaa,ccc,ddd | styleone
fff,ggg | styletwo
xyx,dkk | styleone
please help using some linq query

Group items by stylename property, then project each group ptext values into string joined with coma
list.GroupBy(x => x.stylename)
.Select(g => new {
stylename = g.Key,
ptext = String.Join(",", g.Select(x => x.ptext))
});
Or with query syntax:
from x in list
group x by x.stylename into g
select new { // or new YourItemType {
stylename = g.Key,
ptext = String.Join(",", g.Select(x => x.ptext))
}
You can of course create objects of your list items type instead of anonymous objects.

Related

How do I query array element in impala?

I created a table in impala ,the have two columns ,
+-----------+---------------------+---------+
| name | type | comment |
+-----------+---------------------+---------+
| unique_id | string | |
| cmap | array<struct< | |
| | fieldname:string, | |
| | fieldid:string, | |
| | fielddata:string | |
| | >> | |
+-----------+---------------------+---------+
i need set the conditions for cmap to query unique_id, such as
(fieldname="ip"and fielddata="192.168.1.145") and(fieldname="wid" and fielddata="15")
i wrote this sql but can't query it, but i inserted the data in the table
sql:
select unique_id from s_click_parquet,s_click_parquet.cmap as lst where ( fieldname="ip" and fieldData="192.168.1.145") and(fieldname="wid" and fielddata="15");
You typically access arrays with square brackets, but you can have an array of structs, not just an array.
You can INLINE() an array of struct to make it multiple rows, as well as splitting the fields to queryable columns.
Then you can query it as you would a normal table.
Play around with
select *
from table1 t
lateral view inline(cmap) a;
From my work collogue:
so rule of thumb is structs can get accessed via strucName., while arrays have to be joined*
So if we have the following types from an avro schema:
case class Visit (var Patient: PatientType = null)
case class PatientType(
var Name: String = null,
var Address: List[AddressType] = null
)
case class AddressType(
var Line1: String = null,
var Line2: String = null,
var City: String = null,
var State: String = null,
var ZipCode: String = null
)
You would query for patient name and address like:
select vis.patient.name, addr.item.*
from visit vis
left join vis.patient.address addr

How to get rows by LINQ

Everybody.
I have following data.
DATE|COLUMN1|COLUMN2|VALUE
--------------------------
1 |A |D |100
--------------------------
2 |A |D |200
--------------------------
1 |B |D |150
I need get rows where DATE does have max value. Like that
DATE|COLUMN1|COLUMN2|VALUE
--------------------------
2 |A |D |50
--------------------------
1 |B |D |150
Sorry for my bad English.
As I can understand, you need max date for every group of column1 -
for that you can go with
var custsLastAccess = from c in db.data //data is your table
group c by c.COLUMN1 into grp
select grp.OrderByDescending(c => c.DATE).FirstOrDefault();
You can first select the max from the LINQ and then select by this max:
var max = rows.Max(a => a.DATE);
var result = rows.Where(a => a.DATE == max).ToList();
Simplier:
var result = rows.Where(a => a.DATE == rows.Max(b => b.DATE)).ToList();
Optimizer/compiler should optimize the query above.
Expression:
var result = from r in rows
where r.DATE == rows.Max(a => a.DATE)
select r;
I found solve. The following code decided mine task.
public override IQueryable<ENTITY> Get()
{
return base.Get().GroupBy(x => new { x.COLUMN1, x.COLUMN2 }, (key, group) => new
{
row = group.OrderByDescending(x => x.DATE).FirstOrDefault()
}).Select(x => x.row);
}

Can not join tables with LINQ

I will glad if you help me
I have 4 tables:
http://i.stack.imgur.com/MMIdn.png
I need to get a ApptNumber from Condo and ContractID from Contract
If no contracts assigned to the condo, leave it blank.
Like this:
| Cnds | Cntrcts |
|--------|-----------|
| 101 | contr1 |
| 102 | contr2 |
| 103 | contr3 |
| 104 | |
| 105 | |
| 106 | contr4 |
| 107 | |
|--------|-----------|
I tried to do it like this, but failed to get the right result
var q =
from c in condos
join b in buildings on c.buildingID equals b.BuildingId
join t in tenants on b.BuildingId equals t.buildingID
join ct in contracts on t.TenantID equals ct.tenantID into result
from subresult in result.DefaultIfEmpty()
select new
{
c.apptNumber,
contract = (subresult == null ? 0 : subresult.ContractID)
};
Sincerely
from c in context.Condos
join ct in context.Contracts on c.apptNumber equals ct.SuiteNo into j
//it could be another field for joining
from d in j.DefaultIfEmpty()
select new {c.apptNumber, Contract = d != null ? d.ContractId : null}
Thank you guys. It was my fault to use multiple JOINS in first place.
I resolved the issue like that:
var q =
from c in condos
join ct in contracts on c.apptNumber equals ct.SuiteNo into j //it could be another field
from d in j.DefaultIfEmpty()
select new { c.apptNumber, Contract = d != null ? d.ContractID : 0 };
var q2 = q.GroupBy(c => c.apptNumber).Select(group => group.First());

How to write a left join with many to many relationship

I have the following entities:
Entity: Department
DepartmentId (int)
Name (int)
SuperiorDepartmentId (int, foreign key to department entity)
DepartmentPermissions (ICollection)
Entity: DepartmentPermission
DepartmentId (int)
UserId (int)
Permission (String)
Departments (ICollection)
Entity: User
UserId (int)
Name (string)
DepartmentPermissions (ICollection)
I need to return in my query all of the departments (including the ones where the user don't have permission) and the name of the permission when the user has any.
Department
DepartmentId | Name | SuperiorDepartmentId
1 | Sales | null
2 | Internal Sales | 1
3 | Marketing | null
DepartmentPermissions
DepartmentId | User Id | Permission
1 | 2 | r
2 | 2 | rw
1 | 3 | rw
User
UserId | Name
1 | John
2 | Mary
3 | Paul
If I ask for the data for user Mary (id=2), we shoud have as result set:
DepartmentId | Name | SuperiorDepartmentId | Permission
1 | Sales | null | r
2 | Internal Sales | 1 | rw
3 | Marketing | null | null
How can I do this?
I'm going to presume the existence of a navigation property Department.DepartmentPermissions.
var query = from d in context.Departments
select new {
Department = d,
Permissions = d.DepartmentPermissions
.Where(dp => dp.UserId == 2)
.Select(p => p.Permission)
};
var result = query.AsEnumerable()
.Select(x =>
new {
x.Department.DepartmentId,
x.Department.Name,
x.Department.SuperiorDepartmentId,
Permissions =
string.Join(", ", x.Permissions.DefaultIfEmpty("null")
}
First the raw data are collected (query), then the final results are composed in memory. The latter is done because EF won't allow string.Join in a LINQ query. Your data structure allows for more than one permission per user in one department, hence the string.Join.
If you're absolutely sure that there will always be one permission you can do
Permission = d.DepartmentPermissions.Where(dp => dp.UserId == 2)
.Select(p => p.Permission)
.FirstOrDefault()
in query, and your done.

Select nested list

I have three tables in DB like this:
products
+----+------+
| id | name |
+----+------+
| 1 | prod1|
+----+------+
values
+----+---------+---------+
| id | value | alias |
+----+---------+---------+
| 1 | 10 g | 10m |
+----+---------+---------+
prdoucts_values
+---------------+---------+
| product_id | value_id|
+---------------+---------+
| 1 | 1 |
+---------------+---------+
How select all products from DB, that have any value from List<Values.alias> ?
List<decimal> aliases = ...
var query = db.Products.Where(p => p.Values.Any(v => aliases.Contains(v.Alias)));
Or (if you don't have navigation properties)
var query = from p in db.Products
join pv in db.ProductsValues on p.ProductId equals v.ProductId
join v in db.Values on pv.ValueId equals v.ValueId into values
where values.Any(v => aliases.Contains(v.Alias))
select p
If you are using EF (database first) then the table prdoucts_values is not part of the conseptual model.
instead EF givs you a direct path from Products to Values
Therefore you can write a query that look like this:
var lst = new List<Products>();
lst = db.Products.Where(c => c.Values.Any()).ToList();

Resources