I am trying to construct a navigation menu using a Categories table from my db.
I have a similar layout as below in Categories table.
public List<Category> CategoryData = new List(new Category[] {
new Category{ CategoryId = 1, Name = "Fruit", ParentCategoryId = null},
new Category{ CategoryId = 2, Name = "Vegetables", ParentCategoryId = null},
new Category{ CategoryId = 3, Name = "Apples", ParentCategoryId = 1},
new Category{ CategoryId = 4, Name = "Bananas", ParentCategoryId = 1},
new Category{ CategoryId = 5, Name = "Cucumber", ParentCategoryId = 2},
new Category{ CategoryId = 6, Name = "Onions", ParentCategoryId = 2}
); }
The above should return something like
Fruit (parent)
"===Apples, Bananas (child)
Vegetables (parent)
"===Cucumber, Onions (child)
I need to be able to pass this as some kind of 'grouped' (grouped by parentid) collection to my View.
How to do this?
It would seem like this is a good example of when translating your model to a viewModel would come in handy. As you could create a collection of CategoryViewModel which have a Childrens property of CategoryViewModel using the same technique describe by #thomas.
public class CategoryViewModel
{
public int CategoryId { set; get; }
public string CategoryName { set; get; }
public int? ParentCategoryId { set; get; }
public IEnumerable<CategoryViewModel> Children { set; set; }
}
public static IEnumerable<CategoryViewModel> GetAllCategoryViewModel(IList<Category> categories)
{
var query = GetChildren(null, categories);
return query.ToList();
}
public static IEnumerable<CategoryViewModel> GetChildren(int? parentId, IList<Category> categories)
{
var children = from category in categories
where category.ParentCategoryId == parentId
select
new CategoryViewModel
{
CategoryId = category.CategoryId,
CategoryName = category.CategoryName,
ParentCategoryId = category.ParentCategoryId,
Children = GetChildren(category.CategoryId, categories)
};
return children;
}
How about something like:
private void Test()
{
var categoryData = new List
{
new Category {CategoryId = 1, Name = "Fruit", ParentCategoryId = null},
new Category {CategoryId = 2, Name = "Vegetables", ParentCategoryId = null},
new Category {CategoryId = 3, Name = "Apples", ParentCategoryId = 1},
new Category {CategoryId = 4, Name = "Bananas", ParentCategoryId = 1},
new Category {CategoryId = 5, Name = "Cucumber", ParentCategoryId = 2},
new Category {CategoryId = 6, Name = "Onions", ParentCategoryId = 2}
};
var query = from category in categoryData
where category.ParentCategoryId == null
select category;
foreach ( var item in query )
{
Debug.WriteLine( string.Format( "{0} (parent)", item.Name ) );
Debug.WriteLine( GetChildren(item.CategoryId, categoryData ) );
}
}
private static string GetChildren( int parentCategoryId, IEnumerable categoryData)
{
var children = ( from category in categoryData
where category.ParentCategoryId == parentCategoryId
select category.Name ).ToArray();
return string.Format( "==={0} (child)", string.Join( ", ", children ) );
}
var list = from a in CategoryData
join b in CategoryData on a.ParentCategoryId equals b.CategoryId into c
from d in c.DefaultIfEmpty()
where d != null
select new {
a.CategoryId,
a.Name,
a.ParentCategoryId,
ParentName = d.Name
};
returns
CategoryId Name ParentCategoryId ParentName
3 Apples 1 Fruit
4 Bananas 1 Fruit
5 Cucumber 2 Vegetables
6 Onions 2 Vegetables
You can then loop through it in your view and format accordingly.
Related
What is the most efficient way to
a) retrieve all children objects from multiple parent types, and
b) know what the parent type is and the exact parent Id for each child?
Currently this is what I'm doing and it's incredibly inefficient, at least the part where I find the specific parent of each child.
public class ChildModel
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ParentType1Model
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ChildModel> Children { get; set; }
}
public class ParentType2Model
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ChildModel> Children { get; set; }
}
//Get all ChildModels from ParentType1
var parentType1Children = db.ParentType1Models
.SelectMany(x => x.Children)
.ToList();
listOfChildModels.AddRange(parentType1Children);
//Get all ChildModels from ParentType2
var parentType2Children = db.ParentType2Models
.SelectMany(x => x.Children)
.ToList();
listOfChildModels.AddRange(parentType2Children);
//Find the parent for each ChildModel
foreach (var child in listOfChildModels)
{
ParentType1Model parentType1ModelCheck = null;
ParentType2Model parentType2ModelCheck = null;
parentType1ModelCheck = await db.ParentType1Models
.Where(p => p.Children
.Any(i => i.Id == child.Id))
.FirstOrDefaultAsync();
//If first check is null, then move to second check
if (taskProjectModelCheck == null)
{
parentType2ModelCheck = await db.ParentType2Models
.Where(p => p.Children
.Any(i => i.Id == child.Id))
.FirstOrDefaultAsync();
}
//Now record the parent type and parent Id in an object containing the original ChildModel and it's parent's info (to be used later for various things)
ChildViewModel childViewModel = new ChildViewModel();
childViewModel.ChildModel = child;
if (parentType1ModelCheck != null)
{
childViewModel.ParentType = "ParentType1";
childViewModel.ParentModelId = parentType1ModelCheck.Id;
}
else if (parentType2ModelCheck != null)
{
childViewModel.ParentType = "ParentType2";
childViewModel.ParentModelId = parentType2ModelCheck.Id;
}
}
How about something like this?
var ids1 = from p in db.ParentType1Models
from c in p.Children
select new
{
parentId = p.Id,
parentName = p.Name,
childName = c.Name,
childId = c.Id,
ParentType = "One"
};
var ids2 = from p in db.ParentType2Models
from c in p.Children
select new
{
parentId = p.Id,
parentName = p.Name,
childName = c.Name,
childId = c.Id,
ParentType = "Two"
};
var results = ids1.Union(ids2).ToList();
I ended up using raw SQL, and it is extremely fast.
By writing a query directly against the database, I was able to go straight to the many to many relationship tables that get created by Entity Framework when I set up the ParentTypeXModels and ChildModels.
result = dbContext.Database.SqlQuery<ANewChildObject>(
"select
ParentModelId = pm.Id,
Id = c.Id,
ParentType = 'ParentType1'
from dbo.ChildModels c
JOIN dbo.ParentType1ModelsChildModels pmT ON c.Id = pmT.ChildModel_Id
JOIN dbo.ParentType1Models pm on pmT.ParentType1Model_Id = pm.Id
UNION ALL
select
ParentModelId = pm.Id,
Id = c.Id,
ParentType = 'ParentType2'
from dbo.ChildModels c
JOIN dbo.ParentType2ModelsChildModels pmT ON c.Id = pmT.ChildModel_Id
JOIN dbo.ParentType2Models pm on pmT.ParentType2Model_Id = pm.Id"
).ToList();
I need to convert:
SELECT Author.AuthorName, Author.AuthorSurname , Category.CategoryName, COUNT(Record.IdCategory) AS Ilosc
FROM ((Record
INNER JOIN Author ON Author.Id = Record.IdAuthor)
INNER JOIN Category ON Category.Id = Record.IdCategory)
GROUP BY AuthorName, AuthorSurname, CategoryName
Order by Ilosc ASC
To lambda.
I have created
public class SortModel
{
public string Imie { get; set; }
public string Nazwisko { get; set; }
public string Kategoria { get; set; }
}
For now i wrote this code but i need to add .groupby:
public List<SortModel> GetAuthorCategories()
{
var context = new KolekcjaPlytEntities2();
List<SortModel> queryAllCustomers = context.Record
.Join( context.Author, r => r.IdAuthor, a => a.Id, (r, a) => new {r, a})
.Join(context.Category, c => c.r.IdCategory, cn => cn.Id, (c, cn) => new SortModel()
{
Imie = c.a.AuthorName,
Nazwisko = c.a.AuthorSurname,
Kategoria = cn.CategoryName,
})
.ToList();
return queryAllCustomers;
}
Thanks.
You could use LINQ query expression like this one.
List<SortModel> queryAllCustomers = (from r in context.Record
join a in context.Author on r.IdAuthor equals a.Id
join c in context.Category on r.IdCategory equals c.Id
group new {a, c} by new {a.AuthorName, a.AuthorSurname , c.CategoryName} into gruopAC
let countCategory= gruopAC.Count()
orderby countCategory
select new SortModel
{
Imie=gruopAC.Key.AuthorName,
Nazwisko =gruopAC.Key.AuthorSurname ,
Kategoria =gruopAC.Key.CategoryName,
Ilosc=countCategory}).ToList()
Suppose you have
public class SortModel
{
public string Imie { get; set; }
public string Nazwisko { get; set; }
public string Kategoria { get; set; }
public int Ilosc { get; set; }
}
You can try this code:
public List<SortModel> GetAuthorCategories()
{
var context = new KolekcjaPlytEntities2();
List<SortModel> queryAllCustomers = context.Record
.Join(context.Author, r => r.IdAuthor, a => a.Id, (r, a) => new {r, a})
.Join(context.Category, ar => ar.r.IdCategory, c => c.Id, (ar, c) => new {
r = ar.r,
a = ar.a,
c = c
})
// Here you have a IEnumerable<dynamic> of items { a = Author, c = Category, r = Record }
// All items with same { a.Id, c.Id } are grouped
.GroupBy(x => new { x.a.Id, x.c.Id }, (x, items) => new SortModel()
{
// First() cannot return null, we have inner join everywhere
Imie = items.First().a.AuthorName,
Nazwisko = items.First().a.AuthorSurname,
Kategoria = items.First().c.CategoryName,
Ilosc = items.Count()
})
.ToList();
return queryAllCustomers;
}
Here i have a problem with data table to convert json. This is my class called SearchCollection
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public int ClassGroupId { get; set; }
public string ClassName { get; set; }
public int ClassNumber { get; set; }
public int BookTypeId { get; set; }
public string BookType { get; set; }
I have collected a data from store procedure and pushed into the datatable, thats why am using ConvertToDatatable(), that time i got a datatable which contains 3 tables data
static DataTable ConvertToDatatable(IEnumerable<SearchCollection> list)
{
var dt = new DataTable();
dt.Columns.Add("CategoryId");
dt.Columns.Add("CategoryName");
dt.Columns.Add("ClassGroupId");
dt.Columns.Add("ClassName");
dt.Columns.Add("ClassNumber");
dt.Columns.Add("BookTypeId");
dt.Columns.Add("BookType");
foreach (var item in list)
{
var row = dt.NewRow();
row["CategoryId"] = item.CategoryId;
row["CategoryName"] = item.CategoryName;
row["ClassGroupId"] = item.ClassGroupId;
row["ClassName"] = item.ClassName;
row["ClassNumber"] = item.ClassNumber;
row["BookTypeId"] = item.BookTypeId;
row["BookType"] = item.BookType;
dt.Rows.Add(row);
}
return dt;
}
this contain 3 tables data.
So.. this is have tried to group the data, but here am getting the answer like category on top inside category shows booktype and inside booktype shows list of classnames, but i want 3 set of data
category {},booktype{},classnames{}
var result = rows.GroupBy(r => new { x = r["CategoryId"], y = r["CategoryName"] }).Select(g => new
{
CategoryId = g.Key.x,
CategoryName = g.Key.y,
BookTypes = g.GroupBy(r => new { h = r["BookTypeId"], i = r["BookType"] }).Select(g1 => new
{
BookTypeId = g1.Key.h,
BookType = g1.Key.i,
ClassNames = g1.Select(r => new
{
ClassGroupId = r["ClassGroupId"],
ClassName = r["ClassName"],
ClassNumber = r["ClassNumber"]
}),
}),
});
Rusult
This is my result
{ CategoryId:1 CategoryName:CD ClassGroupId:15 ClassName:I ClassNumber:1 BookTypeId:1 BookType:General CD}
{ CategoryId:2 CategoryName:DVD ClassGroupId:16 ClassName:II ClassNumber:2 BookTypeId:2 BookType:General DVD}
{ CategoryId:3 CategoryName:Book ClassGroupId:17 ClassName:III ClassNumber:3 BookTypeId:3 BookType:General Books}
But i want the result like this
+ Category={ CategoryId:1 CategoryName:CD
CategoryId:2 CategoryName:DVD
CategoryId:3 CategoryName:Book }
ClassGroup={ClassGroupId:15 ClassName:I ClassNumber:1
ClassGroupId:16 ClassName:II ClassNumber:2
ClassGroupId:17 ClassName:III ClassNumber:3}
BookType{ BookTypeId:1 BookType:General CD
BookTypeId:2 BookType:General DVD
BookTypeId:3 BookType:General Books
}
here my result is booktype is under category and classname under booktype. but i want the result just like 3 groups of records in single json, any one help just like category grouped collection, class grouped collection and book type collection in single json data.
Is this what you're looking for?
var result = new
{
Category = rows
.GroupBy(r => new
{
x = r["CategoryId"],
y = r["CategoryName"]
})
.Select(g => new
{
CategoryId = g.Key.x,
CategoryName = g.Key.y
}),
ClassGroup = rows
.GroupBy(r => new
{
x = r["ClassGroupId"],
y = r["ClassName"],
z = r["ClassNumber"]
})
.Select(g => new
{
ClassGroupId = g.Key.x,
ClassName = g.Key.y,
ClassNumber = g.Key.z
}),
BookType = rows
.GroupBy(r => new
{
x = r["BookTypeId"],
y = r["BookType"]
})
.Select(g => new
{
BookTypeId = g.Key.x,
BookType = g.Key.y
})
};
Fiddle: https://dotnetfiddle.net/h9qXqc
I have three models (Applicant, Meeting, City) and I joined three of them. I want to get Count by grouping MeetingId in Applicant model. Here are my models and method I use for populating Dropdownlist in Controller. So, like the "Expired" property in the Controller, how can I obtain the count for the MeetingId by grouping on "TotalMeetingById" property?
Applicant Model:
public class Applicant
{
public int ApplicantID { get; set; }
public DateTime? SubmitDate { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int MeetingId { get; set; }
}
Meeting Model:
public class Meeting
{
public int MeetingID { get; set; }
public string MeetingName { get; set; }
public DateTime MeetingStartDate { get; set; }
public DateTime? MeetingEndDate { get; set; }
public int? TotalParticipant { get; set; }
public int? MeetingCityId { get; set; }
public int? ParticipantCityAId { get; set; }
public int? ParticipantCityBId { get; set; }
}
City Model:
public class City
{
public int CityID { get; set; }
public string CityName { get; set; }
}
Controller:
private void PopulateDropDownList()
{
var meetingsQuery = repository.Meetings
.GroupJoin(repository.Cities, m => m.MeetingCityId, c => c.CityID, (m, c) => new { m, cA = c.DefaultIfEmpty() })
.SelectMany(z => z.cA.Select(cA => new { m = z.m, cA }))
.GroupJoin(repository.Cities, m => m.m.ParticipantCityAId, c => c.CityID, (m, c) => new { m.m, m.cA, cB = c.DefaultIfEmpty() })
.SelectMany(w => w.cB.Select(cB => new { m = w.m, cA = w.cA, cB }))
.GroupJoin(repository.Cities, m => m.m.ParticipantCityBId, c => c.CityID, (m, c) => new { m.m, m.cA, m.cB, cC = c.DefaultIfEmpty() })
.SelectMany(t => t.cC.Select(cC => new { m = t.m, cA = t.cA, cB = t.cB, cC }))
.Select(
m =>
new
{
CityID = m.cA.CityID,
CityName = m.cA.CityName,
MeetingDate = m.m.MeetingStartDate,
MeetingName = m.m.MeetingName,
NameofMeetingCityIdA = m.cA != null ? m.cA.CityName : null,
NameofMeetingCityIdB = m.cB != null ? m.cB.CityName : null,
NameofMeetingCityIdC = m.cC != null ? m.cC.CityName : null
})
.OrderBy(x => x.CityID)
.AsEnumerable()
.Select(
i => new
{
Value = i.CityID.ToString(),
DisplayValue = string.Format("{0} ({1:dd MMMM yyyy})", i.NameofMeetingCityIdA, i.MeetingDate),
Expired = i.MeetingDate < DateTime.UtcNow,
TotalMeetingById= ??? >>> I cannot get the total count for the related MeetingId at here
}
).ToList();
var selectItems = new List<MyHelpers.MySelectItem>(meetingsQuery.Count);
foreach (var record in meetingsQuery)
{
var item = new MyHelpers.MySelectItem
{
Text = record.DisplayValue,
Value = record.Value
};
if (record.Expired)
{
item.Class = "disabled";
item.Disabled = "disabled";
}
selectItems.Add(item);
}
ViewBag.MeetingData = selectItems;
}
Here are sample data for models:
Applicant:
ApplicantID : 100
SubmitDate : 01/11/2013
Name : Christof
Surname : Jahnsen
MeetingId : 1
Meeting:
MeetingID : 1
MeetingName : City Information Days
MeetingStartDate : 01/01/2014
MeetingEndDate : 02/01/2014
TotalParticipant : 2 (for example)
MeetingCityId : 500
ParticipantCityAId : 501
ParticipantCityBId : 502
City:
CityID : 500 / 501 / 502
CityName : London / Paris / NY
Update -----------------------------------------------------------------------
Razor:
#Html.LabelFor(m => m.Applicant.MeetingId)
#Html.MyDropdownListFor(m => m.Applicant.MeetingId, ViewBag.MeetingData as List<MyHelpers.MySelectItem>, "---- Select ----",
new { name = "meetingId", id = "meetingId" })
#Html.ValidationMessageFor(m => m.Applicant.MeetingId, null, new { #class = "ValidationErrors" })
Controller:
public ViewResult Add()
{
PopulateDropDownList();
ApplicantViewModel model = new ApplicantViewModel
{
Applicant = new Applicant(),
Applicants = repository.Applicants,
Lookups = repository.Lookups,
Cities = repository.Cities
.ToList()
};
return View(model);
}
[HttpPost]
public ActionResult Add(ApplicantViewModel model)
{
ApplicantViewModel viewModel;
if (ModelState.IsValid)
{
model.Applicant.SubmitDate = DateTime.Now;
repository.SaveApplicant(model.Applicant);
PopulateDropDownList(model.Applicant);
return View("Completed", ViewBag.ApplicantId = model.Applicant.ApplicantID);
}
else
{
// there is something wrong with the data values
PopulateDropDownList();
TempData["message"] = "Please try again.";
viewModel = new ApplicantViewModel
{
Applicant = new Applicant(),
Applicants = repository.Applicants,
Lookups = repository.Lookups,
Cities = repository.Cities
.ToList()
};
return View(viewModel);
}
}
You need grouping, so use something like this
var meetingsQuery = from meeting in repository.Meetings
join cityA in repository.Cities on meeting.MeetingCityId equals cityA.CityID into CitiesA
join cityB in repository.Cities on meeting.ParticipantCityAId equals cityB.CityID into CitiesB
join cityC in repository.Cities on meeting.ParticipantCityBId equals cityC.CityID into citiesC
from cityA in citiesA.DefaultIfEmpty()
from cityB in citiesB.DefaultIfEmpty()
from cityC in citiesC.DefaultIfEmpty()
orderby cityA.CityID
select new
{
CityID = cityA.CityID,
CityName = cityA.CityName,
MeetingDate = meeting.MeetingStartDate,
MeetingName = meeting.MeetingName,
NameofMeetingCityIdA = cityA != null ? cityA.CityName : null,
NameofMeetingCityIdB = cityB != null ? cityB.CityName : null,
NameofMeetingCityIdC = cityC != null ? cityC.CityName : null
}
var meetings = from meeting in meetingsQuery.AsEnumerable()
group meeting by new {
meeting.CityID,
meeting.MeetingDate,
meeting.NameofMeetingCityIdA
} into grouppedMeeting
select new {
Value = grouppedMeeting.Key.CityID.ToString(),
DisplayValue = string.Format("{0} ({1:dd MMMM yyyy})", grouppedMeeting.Key.NameofMeetingCityIdA, grouppedMeeting.Key.MeetingDate),
Expired = grouppedMeeting.Key.MeetingDate < DateTime.UtcNow,
TotalMeetingById= grouppedMeeting.Count()
}
UPDATE
in this code
// there is something wrong with the data values
PopulateDropDownList();
TempData["message"] = "Please try again.";
viewModel = new ApplicantViewModel
{
Applicant = new Applicant(),
Applicants = repository.Applicants,
Lookups = repository.Lookups,
Cities = repository.Cities
.ToList()
};
return View(viewModel);
you don't mark any item as selected, you only add items to viewbag
Finally I solved the problem with the help of using an external method out of Lambda expression.
private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
var meetingsQuery = repository.Meetings
.GroupJoin(repository.Cities, m => m.MeetingCityId, c => c.CityID, (m, c) => new { m, cA = c.DefaultIfEmpty() })
.SelectMany(z => z.cA.Select(cA => new { m = z.m, cA }))
.GroupJoin(repository.Cities, m => m.m.ParticipantCityAId, c => c.CityID, (m, c) => new { m.m, m.cA, cB = c.DefaultIfEmpty() })
.SelectMany(w => w.cB.Select(cB => new { m = w.m, cA = w.cA, cB }))
.GroupJoin(repository.Cities, m => m.m.ParticipantCityBId, c => c.CityID, (m, c) => new { m.m, m.cA, m.cB, cC = c.DefaultIfEmpty() })
.SelectMany(t => t.cC.Select(cC => new { m = t.m, cA = t.cA, cB = t.cB, cC }))
.Select(
m =>
new
{
TotalParticipant = m.m.TotalParticipant,
MeetingID = m.m.MeetingID,
CityID = m.cA.CityID,
CityName = m.cA.CityName,
MeetingDate = m.m.MeetingStartDate,
MeetingName = m.m.MeetingName,
NameofMeetingCityIdA = m.cA != null ? m.cA.CityName : null,
NameofMeetingCityIdB = m.cB != null ? m.cB.CityName : null,
NameofMeetingCityIdC = m.cC != null ? m.cC.CityName : null
})
.OrderBy(x => x.CityName)
.AsEnumerable()
.Select(
i => new
{
Value = i.MeetingID.ToString(),
DisplayValue = string.Format("{0} ({1:dd MMMM yyyy})", i.NameofMeetingCityIdA, i.MeetingDate)),
Expired = i.MeetingDate < DateTime.UtcNow,
MaksimumCount = i.TotalParticipant,
CurrentCount = GetMeetingCount(i.MeetingID)
}
).ToList();
var selectItems = new List<MyHelpers.MySelectItem>(meetingsQuery.Count);
foreach (var record in meetingsQuery)
{
var item = new MyHelpers.MySelectItem
{
Text = record.DisplayValue,
Value = record.Value
};
//If Meeting Date is expired or Count for the current record >= Total Participant
if (record.Expired || record.CurrentCount >= record.MaksimumCount)
{
item.Class = "disabled";
item.Disabled = "disabled";
}
selectItems.Add(item);
}
ViewBag.MeetingData = selectItems;
}
public int GetMeetingCount(int meetingId)
{
return repository.Applicants.Count(x => x.MeetingId == meetingId);
}
The first running of the code I encountered "There is already an open DataReader associated with this Command which must be closed first." error. But adding "MultipleActiveResultSets=True;" parameter to teh ConnectionString solved this problem and now it works properly.
Currently I have 2 database tables which looks like:
--------------- --------------------
| Categories | | Item_Categorys |
--------------- --------------------
| id | | id |
| Title | | Category_ID |
--------------- | Item_ID |
--------------------
I have a Model which displays Checkboxes on my View which is like
LocalCategoryModel
-------------------
int categoryid
string category_title
bool ischecked
I'm trying to get all the items categorys from the table and then get all the category rows then crosscheck them to where if there's a category item, it Puts it in a IEnumerable. So at the end, the LocalCategory has all the categories and then the ischecked is set to true or false depending on if it has a row in the Item_Categorys sql table.
public class LocalCategoryModel
{
public int categoryid { get; set; }
public string category_title { get; set; }
public bool ischecked { get; set; }
}
public IEnumerable<LocalCategoryModel> getSourec()
{
IEnumerable<LocalCategoryModel> query = from tbcat in Categories
join tbitem_cat in dc.Item_Categorys
on tbcat.id equals tbitem_cat.Category_ID into ct
from tbitem_cat in ct.DefaultIfEmpty()
select new LocalCategoryModel
{
categoryid = tbcat.id,
category_title = tbcat.Title,
ischecked = tbitem_cat == null ? false : true
};
return query;
}
I'll get you some example.
It helps you.
I refer to a book named 'C# 4.0 IN A NUTSHELL'.
// outer collection
customers.Join (
purchases, // inner collection
c => c.ID, // outer key selector
p => p.CustomerID, // inner key selector
(c, p) => new { c, p } ) // result selector
.OrderBy (x => x.p.Price)
.Select (x => x.c.Name + " bought a " + x.p.Description);
from c in customers
join p in purchases on c.ID equals p.CustomerID
select new { c.Name, p.Description, p.Price };
class Category { public int id; public string Title; };
class CategoryItem { public int category_id; public int item_id; }
class Category { public int id; public string title; };
// Create categories list
var categories = new List<Category>() { new Category { id = 1, title = "First" }, new Category { id = 2, title = "Second" } };
// Create categories items list
var categories_items = new List<CategoryItem>() { new CategoryItem { category_id = 1, item_id = 2 } };
// All categories in categories_items
var categories_items_set = categories_items.Select(x => x.category_id).Distinct();
// Final query
var q = categories.Select(x => new { categoryid = x.id, category_title = x.title, ischecked = categories_items_set.Contains(x.id) });
q.ToList()
// output
categoryid = 1, category_title = First, ischecked = True
categoryid = 2, category_title = Second, ischecked = False