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 Articles
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- Optgroup for dropdownlist in MVC 4
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- The source contains no DataRows
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Entity Framework 6 Code First Custom Functions
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How to bind LINQ data to dropdownlist
- Is it possible to express this code in LINQ?
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- How to dynamically add row(s) in SQL query result
- Get The Full List Of Objects In A Dictionary Values
- How to achieve groupby in LINQ
- LINQ to SQL Help
- Better way to calculate the SUM of one, two and three consecutive items
- Select multiple table with Linq to Sql
- What's the best way to write [0..100] in C#?
- How to return from ConditionalExpression.IfThen in tree with MethodCallExpression / lambda?
- Expressions: Breaking up a complex linq query
- SQL data context with inner join
- API call JSON File and create Database
- Building the 'where' clause in a Linq query
- Select random images using Linq/C#?
- MongoDB random results in C# LINQ
- Medium to Advance LINQ/EntityFramework Query question. taggings items
- Should parameters/returns of collections be IEnumerable<T> or T[]?
- How to convert a linq query to ToList() to use extended properties
- linq condition in select statement
- linq compare column data between two dates
- C# LINQ: Get items with max price