score:1
As Stephen Muecke said in the comment section that Projects and Clients are 2 different things. You should have a Client table and a separate Project table containing a column with foreign key to the Client table. So below solution is according to your existing table structure.
Dummy Schema
DECLARE @clientprj TABLE ([id] int identity(1,1), [name] varchar(30), Desc1 varchar(30), ParentId int, pType varchar(30));
INSERT @clientprj ([name], Desc1, ParentId, pType) VALUES ('client1', '', 0, 'clt'), ('prj1', '', 1, 'prj'), ('prj2', '', 1, 'prj'), ('client2', '', 0, 'clt'), ('prj n', '', 4, 'prj')
and Here is the query
SELECT GroupName, GroupKey, ProjName, ProjId
FROM
(
(
SELECT NAME AS GroupName
,Id AS GroupKey
FROM @clientprj m
WHERE ParentId = 0
) m
FULL OUTER JOIN
(
SELECT NAME AS ProjName
,Id AS ProjId
,ParentId
FROM @clientprj
)
t ON m.GroupKey = t.ParentId
)
WHERE ParentId <> 0
Which return the following output.
GroupName GroupKey ProjName ProjId
client1 1 prj1 2
client1 1 prj2 3
client2 4 prj n 5
and your controller method which calls this query like this -
Model.ddlProject = db.NewMethod
.Select(t => new GroupedSelectListItem
{
GroupName = t.GroupName,
GroupKey = t.GroupKey.ToString(),
Text = t.ProjName,
Value = t.ProjId.ToString()
});
and then bind your dropdownlist. Best of luck...
Source: stackoverflow.com
Related Query
- Optgroup for dropdownlist in MVC 4
- source code for LINQ 101 samples
- ForEach loop with Lambda expression in Razor code MVC 5 For List<T>
- Shortcut LINQ toentity query for the following code in MVC 3
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- MVC Razor brackets inside code block
- Suggestions for designing complex LINQ code
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ Source Code Available
- Ordering Entity Framework items and child items for MVC view
- .NET 4 Code Contracts: "requires unproven: source != null"
- linq - how do you do a query for items in one query source that are not in another one?
- Is there any open source software for converting SQL statements to LINQ?
- MVC orderby on dropdownlist
- ASP.Net MVC error creating model for 2 linked tables
- Advanced LINQ Grouping and Projection Query for Hierarchical Data (EF 4.0 + LINQ + ASP.NET MVC + HighCharts)
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
- MVC Controller: Using LINQ to check for duplicate value already existing in table before Save?
- Determine the source DataContext for a Linq to Sql query
- Code Example for Add DateTime and TimeSpan in EF query
- Could not find an implementation of the query pattern for source type
- mvc fill dropdownlist
- creating Linq to sqlite dbml from DbLinq source code
- Stubbing Code for Test With Linq Expressions and Lambdas
- what is equivalent c# code for following statement?
- Where is the source for System.Linq.Enumerable.ToList()?
- Dynamic "Not" by parameter in LINQ (Or any other code for that matter)
- Are we compromising performance for achieving code readability when using LINQ?
- Better code for avoiding one dictionary - Case Sensitivity Issue
More Query from same tag
- c# Xdocument and descendants with the same name not working
- How to add to target objects using XDocument and Linq?
- Prevent 'NOW()' in LINQ to EF query
- find record from referenced tables of primary key column
- How to create multiply WHERE (and compare) with LINQ?
- LINQ (or something else) to compare a pair of values from two lists (in any order)?
- Get ordered group last item
- Linq - Set int to 0 if it is a minus number
- Linq to EF - populate a (fairly simple) hierarchical object from EDM C#
- Selecting Usernames Uniquely
- Filtration strongly typed result
- LINQ to SQL: select array of arrays of integers in one query
- IQueryable Extension - queryExpression filter is always null
- C# - Add values from list in a fluent interface
- Why does Entity Framework 6 generate complex SQL queries for simple lookups?
- linq max lazy evaluation
- date difference in EF4
- Get value in string from variable containing data from table
- Split collection into multiple collections
- Call c# method in linq query(entity framework)
- Comparing two strings containing multiple dots inside a Linq to Sql query
- c# ASP.Net Core and Linq: aggregate functions MIN and MAX
- Decent Free Linq debugger
- The specified type member 'Date' is not supported in LINQ to Entities. DbFunctions.TruncateTime()
- Filtering lists using LINQ
- How to extract maximum date from a list of random dates, grouped by week, month, quarter or year?
- How use LINQ to find entities where a list of sub entities contains another list
- LINQ nested collection
- Linq query to find date range
- LINQ order by "round robin"