Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am creating a website using asp.net and MVC. I need to populate a select box with values from my database and I was wondering how I would go about doing this?
There are many different ways to do it, but this what I do. In my service class, I do the db select & return a SelectList object
public SelectList GetAsSelectList()
{
var depts = from d in GetAll()
select new
{
Id = d.Id,
Name = d.Name
};
return new SelectList(depts, "Id", "Name");
}
Then in my model, I assign the selectlist to a property:
model.Suppliers = _supplierService.GetAsSelectList();
return View(model);
And finally the View:
#Html.DropDownListFor(model => model.DeliveryStoreId, Model.DeliveryStores)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Please I need to implement distinct in a.CompanyName but it doesn't work. What do I need to do ?
try
{
var listCompany = company.CompanyList();
companyList1.DataSource = listCompany.Select(a => new { a.CompanyName, a.CompanyId }).Distinct();
companyList1.DataTextField = "CompanyName";
companyList1.DataValueField = "CompanyId";
companyList1.DataBind();
}
catch (Exception)
{
throw;
}
You can use with .GroupBy()
Groups the elements of a sequence.
companyList1.DataSource = listCompany
.GroupBy(x => x.CompanyName)
.Select(a => new
{ CompanyName = a.First().CompanyName, CompanyId = a.First().CompanyId });
Above query will group your result by company name and then will select CompanyName and CompanyId of first record
Try out online
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How to convert normal SQL to LINQ query? Is there tools that can do that?? Online Tools
Have you tried something like this (a few joins omitted for brevity):
var result = from s in TooltipsLanguage
join c in TooltipsLanguageSection on s.Id equals c.IdLanguage
join p in TooltipsSection on c.IdSection equals p.Id
join ...
select new MyDestinationObject()
{
Id = s.BusinessEntityID,
Language = s.Language,
IdLanguage = c.IdLanguage,
...
};
This meight be help you.
var temp= edbContext.TooltipsLanguage.select(
c=> new {
TooltipsLanguage.Id,
TooltipsLanguage.Language,
TooltipsLanguage.TooltipsLanguageSection.Id,
TooltipsLanguage.TooltipsLanguageSection.IdLanguage,
TooltipsLanguage.TooltipsLanguageSection.IdSection,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.IdItem,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.IdItem,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.IdText,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.TooltipsText.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.TooltipsText.Texts});
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Ok so I have these tables:
Video
Id
Title
VideoTag
Id
VideoId
TagId
Tag
Id
Name
I need a LINQ to SQL query to perform a search on the Videos, either by the title or by the tags.
For the moment all I have is this for searching by the title:
IQueryable<Video> videos =
from v in ctx.Videos
where v.Title.Contains(SearchString)
select
new Video
{
Id = v.Id,
Title = v.Title
};
Try this:
from v in ctx.Videos
join vt in ctx.VideoTags on v.Id equals vt.VideoId
join t in ctx.Tag on vt.TagId equals t.Id
where v.Title.Contains(SearchString) || t.Name.Contains(SearchString)
select
new Video
{
Id = v.Id,
Title = v.Title
};
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
As in title, how to rewrite this linq query into method syntax?
(from row in recordsEpP
where (!string.IsNullOrEmpty(row.Column20)
&& !string.IsNullOrEmpty(row.Column14)
&& string.IsNullOrEmpty(row.Column8))
select new ReportDto{
Status = "P",
ContractNumber = row.ContractNumber,
Count = 1
}).ToList();
You could try something like this:
recordsEpP.Where(row=> !string.IsNullOrEmpty(row.Column20) &&
!string.IsNullOrEmpty(row.Column14) &&
string.IsNullOrEmpty(row.Column8)
).Select(row => new ReportDto{
Status = "P",
ContractNumber = row.ContractNumber,
Count = 1
}).ToList();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an existing List that countains data
List<string>countries
I want to somehow loop through this data an populate the following collection
IEnumerable<SelectListItem> Countries
How would I go about in doing this?
The simplest method I can think off is using a Linq query:
Countries = countries.Select(country => new SelectListItem {Text = country, Value = country});
You won't have the ability to differ the text from the value, however.
Presuming this is related to mvc... try this
IEnumerable<SelectListItem> Countries = countries.Select(c => new SelectListItem { Text = c, Value = c });
Something like this I think.
IEnumerable<SelectListItem> Countries = countries.Select(c => new SelectListItem
{
Text = c,
Value = c
});