Join 2 Columns with same ID in Linq - c#

My problem is how can i join the selected rows with identical ID but different contacts
This is the output of what I'm doing
| Name | Address | Cellphone | Email |
| John | NY | n/a | johndoe#y.c |
| John | NY | 123456781 | n/a |
and i want my output to be one liner combined
| Name | Address | Cellphone | Email |
| John | NY | 123456781 | johndoe#y.c |
this is my Linq
var an = (from a in db.Info
join b in db.Contact
on a.ID equals b.InfoID
where b.ContactTypeID == 56
|| b.ContactTypeID == 59
select new
{
a.ID,
a.LastName,
a.FirstName,
a.MiddleName,
b.ContactTypeID,
b.Values
}).ToList();
List<Info> wlist = new List<Info>();
foreach (var row in an)
{
Info ci = new Info
{
ID = row.ID,
Name = row.FirstName + " " + row.MiddleName + " " + row.LastName,
ContactType = GetLookupDisplayValById(row.ContactTypeID),
ContactValue = row.Values
};
wlist.Add(ci);
}
return Json(wlist.ToList(), JsonRequestBehavior.AllowGet);
}
I hope someone can help me with this
Thanks :)

You can do it:
from info in db.Info
join contact in db.Contact
on info.ID == contact.InfoID
select new Contact(info, contact)
And your constructor will merge.
public class Contact
{
public int ID { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public int ContactTypeID { get; set; }
public Contact(Info info, Contact contact)
{
ID = info.ID;
LastName = string.IsNullOrEmpty(info.LastName) ? contact.LastName : info.LastName;
}
}

try with GroupBy
var resuts = db.Info.GroupBy(i=>i.ID)
.Select(g=>new Info{
ID = g.Key,
Address = g.Fist().Address,
Cellphone = g.FistOrDefault(x=> x.Cellphone !="n/a"),
Email = g.FistOrDefault(x=> x.Email !="n/a")
}).ToList();

Related

C# linq IQueryable multiple filtering

I cant find a way how to do a multiple filtering with LINQ.
what i want to do:
1.Filter the Version(Achieved)
2.Filter down the CompanyName(filtering/removing duplicates)
3.Get the Latest Timestamp
4.Then add all to a List.
Here is so far the code that i have written(which is not working).
public List<ReleaseStatistics> GetReleaseStatistics(IQueryable<InstallationInformation> input, IQueryable<DBContext.Version> mapping)
{
List<ReleaseStatistics> Releasestats = new List<ReleaseStatistics>();
foreach (var version in mapping)
{
IQueryable<InstallationInformation> QueryResult1 = input.Where(x => x.ProductVersion == version.VersionNumber);
IQueryable<InstallationInformation> QueryResult2 = QueryResult1.GroupBy(x => x.CompanyName).SelectMany(y => y);
List<InstallationInformation> ListofInstallationInformation = QueryResult2.ToList<InstallationInformation>();
if (ListofInstallationInformation.Count >= 1)
{
Releasestats.Add(new ReleaseStatistics
{
ReleaseVersion = version.ReleaseName,
CustomerCount = QueryResult1.Where(x => x.ProductVersion == version.VersionNumber).Count()
});
}
}
return Releasestats;
}
Addition information:
One of the problem is that there are duplicate and i want to Filter/remove them, but i want to get the latest timestamp of each CompanyName and then add it to the list.
the problem is that the line
IQueryable<InstallationInformation> QueryResult2 = QueryResult1.GroupBy(x => x.CompanyName).SelectMany(y => y);
actually does nothing.
Suppose QueryResult1 is
CompanyName | F1 | F2 |
CN1 | f1a | f2a |
CN1 | f1a | f2a |
CN2 | f1b | f2b |
then QueryResult1.GroupBy(x => x.CompanyName) is
Group | Data
CN1 | CompanyName | F1 | F2 |
CN1 | f1a | f2a |
CN1 | f1a | f2a |
CN2 | CompanyName | F1 | F2 |
CN2 | f1b | f2b |
then QueryResult1.GroupBy(x => x.CompanyName).SelectMany(y => y); is again
CompanyName | F1 | F2 |
CN1 | f1a | f2a |
CN1 | f1a | f2a |
CN2 | f1b | f2b |
what you want to do is probably
var QueryResult2 = QueryResult1.GroupBy(x => x.CompanyName).Select(y => new {CompanyName = y.Key, MaxTimestamp = y.Max(z => z.TimeStamp)});
I did it with classes to simulate your query
DBContext context = new DBContext();
List<InstallationInformation> input = new List<InstallationInformation>();
var query = (from m in context.mapping
join i in input on m.VersionNumber equals i.ProductVersion
select new { version = m.VersionNumber, companyName = i.CompanyName}
).ToList();
List<ReleaseStatistics> results = query.GroupBy(x => x.version).Select(x => new ReleaseStatistics() { ReleaseVersion = x.Key, CustomerCount = x.Distinct().Count() }).ToList();
}
}
public class DBContext
{
public List<Version> mapping { get; set; }
}
public class InstallationInformation
{
public int ProductVersion { get; set; }
public string CompanyName { get; set; }
}
public class Version
{
public int VersionNumber { get; set; }
}
public class ReleaseStatistics
{
public int ReleaseVersion { get; set; }
public int CustomerCount { get; set; }
}
HERE WAS MY END RESULT:
public List<ReleaseStatistics> GetReleaseStatistics(IQueryable<InstallationInformation> input, IQueryable<DBContext.Version> mapping)
{
List<ReleaseStatistics> Releasestats = new List<ReleaseStatistics>();
foreach (var version in mapping)
{
IQueryable<InstallationInformation> QueryResult1 = input.Where(x => x.ProductVersion == version.VersionNumber);
IQueryable<string> companynamecollection = QueryResult1.Select(x => x.CompanyName).Distinct();
List<InstallationInformation> listofAggregatedInstallationInformation = new List<InstallationInformation>();
foreach (var item in companynamecollection)
{
var maxdatetime = QueryResult1.Where(x => x.CompanyName == item).Select(x => x.Timestamp).Max();
IQueryable<InstallationInformation> listofresult = QueryResult1.Where(y => y.CompanyName == item && y.Timestamp == maxdatetime);
foreach (var item2 in listofresult)
{
listofAggregatedInstallationInformation.Add(new InstallationInformation
{
InstallationInformationID = item2.InstallationInformationID,
LicenceKey = item2.LicenceKey,
ProductName = item2.ProductName,
ProductVersion = item2.ProductVersion,
CompanyName = item2.CompanyName,
Timestamp = item2.Timestamp,
ImportRunID = item2.ImportRunID
});
}
}
if (listofAggregatedInstallationInformation.Count >= 1)
{
Releasestats.Add(new ReleaseStatistics
{
ReleaseVersion = version.ReleaseName,
CustomerCount = listofAggregatedInstallationInformation.Where(x => x.ProductVersion == version.VersionNumber).Count()
});
}
}
return Releasestats;
}

How to concatenate multiple rows into a single row in ASP.NET MVC EF

I have 3 tables:
Recipe
RecipeIngredient (fk RecipeId)
RecipeTag (fk RecipeId)
Recipe
+-----+------------+-------------+------+
| Id | Name | Ingredients | Tags |
+-----+------------+-------------+------+
| 99 | Mango Sago | | |
| 100 | Tuna Melt | | |
+-----+------------+-------------+------+
RecipeIngredient
+-----+----------+------------+---------------------------------------------+----------+
| Id | Quantity | UOM | Name | RecipeId |
+-----+----------+------------+---------------------------------------------+----------+
| 115 | 2 | Pieces | Whole Ripe Mangoes | 99 |
| 116 | 1 | Pieces | Jolly Coconut Milk, 400ml | 99 |
| 117 | 2 | Tablespoon | Sugar | 99 |
| 118 | 1 | Cup | Cooked Tapioca Pearls | 99 |
| 119 | NULL | NULL | Mango Cubes | 99 |
| 120 | 1 | Pieces | Doña Elena 100% Tuna Shredded 185g, drained | 100 |
| 121 | 2 | Tablespoon | White Onion, chopped | 100 |
| 122 | 2 | Tablespoon | Jolly Real Mayonnaise | 100 |
| 123 | 1 | Tablespoon | Celery or Pickle Relish, finely chopped | 100 |
| 124 | 8 | Pieces | White Bread | 100 |
| 125 | 4 | Pieces | Cheddar or Mozzarella Cheese | 100 |
+-----+----------+------------+---------------------------------------------+----------+
RecipeTag
+----+-----------------+----------+
| Id | Name | RecipeId |
+----+-----------------+----------+
| 72 | Filipino Desert | 99 |
| 73 | Quick Recipe | 99 |
| 74 | Quick Recipe | 100 |
+----+-----------------+----------+
How do I add all the RecipeIngredient to the Ingredients column in the Recipe table and add all the RecipeTag to the Tags column in the Recipe table in the controller?
public JsonResult GetAllRecipes()
{
var recipes = db.Recipes.OrderBy(a => a.Name).ToList();
return new JsonResult { Data = recipes, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
public JsonResult GetAllRecipes()
{
var recipes = (from rec db.Recipes
join ing in db.Ingredients on rec.Id equals ing.RecipeId into subIngrs
from subIngr in subIngrs.DefaultIfEmpty()
join tag in db.RecipeTags on rec.Id equals tag.RecipeId into subTags
from subTag in subTags.DefaultIfEmpty()
order by rec.Name
select new
{
rec.Id,
rec.Name,
Quantity = subIngr == null ? null : subIngr.Quantity,
IngrName = subIngr == null ? null : subIngr.Name,
UOM = subIngr == null ? null : subIngr.UOM,
TagName = subTag == null ? null : subTag.Name
}).ToList()
.GroupBy(x => new { x.Id, x.Name }).Select(x => new
{
x.Key.Id,
x.Key.Name,
Ingredients = string.Join("," x.Where(y => y.IngrName != null).Select(y => $"{y.Quantity} {y.UOM} {y.Name}").Distinct()),
Tags = string.Join("," x.Where(y => y.TagName != null).Select(y => y.TagName).Distinct())
}).ToList();
return new JsonResult { Data = recipes, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
You can use STUFF if you want to do it in sql.
SELECT a.Id,
Recipe = a.Name,
Ingredient = STUFF((
SELECT ','
+ CAST(b.Quantity AS NVARCHAR(10)) + ' '
+ b.UOM + ' '
+ b.Name
FROM dbo.RecipeIngredient b
WHERE a.Id = b.RecipeId
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ''),
Tag = STUFF((
SELECT ',' + c.Name
FROM dbo.RecipeTag c
WHERE a.ID = c.RecipeId
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM Recipe a
Here's a Demo.
To execute the stored procedure and return the results as a json in your controller method
var recipies = db.Database.SqlQuery<RecipeVM>("NameOfStoredProcedure");
return Json(recipes, JsonRequestBehavior = JsonRequestBehavior.AllowGet);
where RecipeVM is
public class RecipeVM
{
public int Id { get; set; }
public string Name { get; set; }
public string Ingredient { get; set; }
public string Tag { get; set; }
}
Assuming you have set up your navigation properties correctly (i.e. Recipe contains public virtual ICollection<RecipieIngredient> Ingredients { get; set; } etc) then to get the data in the concatenated format you want,
public JsonResult GetAllRecipes()
{
var recipes = db.Recipes
.OrderBy(r => r.Name)
.ToList() // this is necessary because we need Linq to Objects for the string formatting
.Select(r => new // can be anonymous objects because we are returning a JsonResult
{
Id = r.Id,
Name = r.Name,
Ingredients = r.Ingredients
.Select(i => string.Format("{0} {1} {2}", i.Quantity, i.UOM, i.Name).TrimStart())
.Aggregate((c, n) => c + ", " + n),
Tags = r.Tags
.Select(t => t.Name)
.Aggregate((c, n) => c + ", " + n)
});
return Json(recipes, JsonRequestBehavior = JsonRequestBehavior.AllowGet);
}
Side note, your RecipeIngredient table indicates nullable values for Quantity and UOM hence the TrimStart. I am assuming that if either Quantity or UOM is null, then the other is also null
Alternatively, (in LINQ to SQL) you can use
var recipes = (from r in db.Recipies
join i in db.RecipeIngredient on r.Id equals i.RecipeId into Ingedients
join t in db.RecipeTag on r.Id equals t.RecipeId into Tags
orderby r.Name
select new
{
Name = r.Name,
Ingedients = Ingedients,
Tags = Tags
}).ToList()
.Select(x => new
{
Name = x.Name,
Ingredients = x.Ingedients
.Select(y => string.Format("{0} {1} {2}", y.Quantity, y.UOM, y.Name).Trim())
.Aggregate((c, n) => c + ", " + n),
Tags = x.Tags
.Select(y => y.Name)
.Aggregate((c, n) => c + ", " + n)
});
return Json(recipes, JsonRequestBehavior = JsonRequestBehavior.AllowGet);
I'm not exactly sure what you're trying to accomplish, but if you're trying to programatically insert this data from user input, you could just iterate over the ingredients/tags and build a string to insert into the relevant tables.
If you're trying to do this from existing data, then I'm not sure EF is the tool to use. I would just write SQL scripts to handle that.

linq where list contains all matched in list

I have three tables Keyword, Product and KeywordProduct.
If I try to filter "the", return A and B products
If I try to filter "matrix", return only B product
but when I filter "the matrix", I get B and A too. I need to get only B record.
that is the code:
var keywordTermList = ("the matrix").Split(' ');
db.Products
.Where(product => product.ProductKeywords.All(k => keywordTermList.Contains(k.Keyword.Name)))
Keyword Table
+-----------+---------+
| KeywordId | Name |
+-----------+---------+
| 1 | awakens |
| 2 | force |
| 3 | the |
| 4 | matrix |
+-----------+---------+
ProductKeyword Table
+------------+-----------+
| KeywordId | ProductId |
+------------+-----------+
| 3(the) | A |
| 2(force) | A |
| 1(awakens) | A |
| 3(the) | B |
| 4(matrix) | B |
+------------+-----------+
Product Table has A and B records.
How would I go about this? How can I get only B when I filter "the matrix".
If you want the subset to fully match the surperset
var query = products.Where(p => !keywordListTerm.Except(p.ProductKeywords.Select(pk => pk.Name)).Any());
Here is full example
public class Program
{
public static void Main(string[] args)
{
Product A = new Product();
A.Name = "A";
A.ProductKeywords = new List<Keyword>() {
new Keyword(){Name = "the"},
new Keyword(){Name = "force"},
new Keyword(){Name = "awakens"}
};
Product B = new Product();
B.Name = "B";
B.ProductKeywords = new List<Keyword>() {
new Keyword(){Name = "the"},
new Keyword(){Name = "matrix"}
};
List<Product> products = new List<Product>() { A, B };
var keywordListTerm = ("the force matrix").Split(' ');
var query = products.Where(p => !keywordListTerm.Except(p.ProductKeywords.Select(pk => pk.Name)).Any());
foreach (var item in query) {
Console.WriteLine(item.Name);
}
Console.ReadKey();
}
}
public class Product {
public string Name = string.Empty;
public List<Keyword> ProductKeywords;
}
public class Keyword
{
public string Name;
}
Hope this helps.

Converting SQL Join Query to Linq query

i want to join different column where the output would be this using linq
| Name | Address | Cellphone | Email |
| John | NY | n/a | johndoe#y.c |
| John | NY | 123456781 | n/a |
And i want my output to be one liner combined
| Name | Address | Cellphone | Email |
| John | NY | 123456781 | johndoe#y.c |
I Tried it on SQL server and this is the Query that answers the needed output
select a.ID, a.Name , a.Address ,(
SELECT wc1.[Values]
FROM Contact as wc1 where wc1.infoID = a.ID and wc1.ContactTypeID = 56) as Email
,(
SELECT wc1.[Values]
FROM Contact as wc1 where wc1.infoID = a.ID and wc1.ContactTypeID = 59) as Cellphone
from Info as a where a.ID = 100
Also tried it on Linq but it produces different row with same ID
var an = (from a in db.Info
join b in db.Contact on a.ID equals b.InfoID
where b.ContactTypeID == 56
|| b.ContactTypeID == 59
select new
{
a.ID,
a.LastName,
a.FirstName,
a.MiddleName,
b.ContactTypeID,
b.Values
}).ToList();
List<InfoList> wlist = new List<InfoList>();
foreach (var row in an)
{
InfoList ci = new InfoList
{
ID = row.ID,
Name = row.FirstName + " " + row.MiddleName + " " + row.LastName,
ContactType = GetLookupDisplayValById(row.ContactTypeID),
ContactValue = row.Values
};
wlist.Add(ci);
}
return Json(wlist.ToList(), JsonRequestBehavior.AllowGet);
Can someone help me translate this to a Linq Statement
Your SQL does not use JOIN, so why are you trying to introduce it in LINQ?
var an = (from a in db.Info
select new
{
a.ID,
a.LastName,
a.FirstName,
a.MiddleName,
Email = db.Contact.FirstOrDefault(b => b.InfoID == a.ID && b.ContactTypeIF == 56).Values,
Cellphone = db.Contact.FirstOrDefault(b => b.InfoID == a.ID && b.ContactTypeIF == 59).Values,
}).FirstOrDefault(x => x.ID == 100);
You can use the Pivot table the mentioned link shows how to use it: http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query

Recursive category and inner join

I want to make recursive category menu for product categories on my web page.Each category must retrieve related first item according to CategoryId on "Product" table but this category should be disappear if category hasn't any product.Actually, i can make to easily with using INNER JOIN for non-recursive category menu.How can i solve this issue?Is there any idea?
I can use a method as follow but this method both amateur and may be null first item.
Category table
+--------------+---------------+------------+
| CategoryId | CategoryName | ParentId |
+--------------+---------------+------------+
| 1 | Cookware | NULL |
+--------------+---------------+------------+
| 2 | Tableware | NULL |
+--------------+---------------+------------+
| 3 | Teapots | 1 |
+--------------+---------------+------------+
| 4 | Cutleries | 3 |
+--------------+---------------+------------+
| 5 | 30pcs Cutlery | 2 |
+--------------+---------------+------------+
Product table
+--------------+--------------+--------------------+------------+
| ProductId | ProductCode | ProductName | CategoryId |
+--------------+--------------+--------------------+------------+
| 1 | G110090 | Teapot | 3 |
+--------------+--------------+--------------------+------------+
| 2 | D220623 | Cutlery Set | 5 |
+--------------+--------------+--------------------+------------+
RecursiveCategory method
public string RecursiveCategory(IEnumerable<Category> category, int? parent)
{
string catNode = string.Empty;
if(category.Any(n=>n.ParentId == parent))
{
catNode += "<ul>";
foreach(Category c in category)
{
catNode += "<li>";
catNode += "<a href='/Detail/" + GetFirstItem(c.CategoryId).ProductId + "'>"+c.CategoryName+"</a>";
catNode += RecursiveCategory(category, c.ParentId);
catNode += "</li>";
}
catNode += "</ul>"
}
return catNode;
}
GetFirstItem method
public Product GetFirstItem(int categoryId)
{
Product prod = new Product();
foreach(Product p in db.Product.Where(n=>n.CategoryId == categoryId))
{
prod.ProductId = p.ProductId;
prod.ProductName = p.ProductName;
...
}
return prod;
}
Try this to construct a hierarchy from a given point (using null in the first call will give you the full tree with the products for each category). As a modification you could make product lazy load if you need to:
public class Category
{
IEnumerable<Category> Children {get;set;}
IEnumerable<Product> Products {get;set;}
}
public IEnumerable<Category> GetCategory(int? parent)
{
var result = new List<Category>();
foreach (var cat in categories.Where(p => p.parentId = parent)
{
var generatedCategory = new Category();
generatedCategory.Children = GetCategory(cat.id);
generatedCategory.Products = products.Where(p => p.CategoryId = cat.CategoryId);
result.Add(generatedCategory);
}
return result;
}
Note: I haven't tested the code just a guide about how to construct it easily.

Categories