populating IEnumerable collection with a List<string> data [closed] - c#

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
});

Related

ASP.NET using Entity Framework display data to drop down list with distinct [closed]

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

In a string list how to split by every 2 character and insert a comma [closed]

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 4 years ago.
Improve this question
I have a list string which has values like below, could you please let me know how to split the list by every 2 character and insert a comma separated and assign the final list to another list.
var list1 = new List<string>() {"DVMNKL"};
var list2 = new List<string>() {"DV","MN","KL"};
Some time list1 can have only 2 character, at that time I should not split, I have to just assign to list2
You can use System.Linq to manage that.
int splitByCount = 2;
string s = new List<string> { "DVMNLS", "DVMNLS" };
var split = s.SelectMant(c => c) //flatten the list of strings to IEnumerable<char>
.Select((c, index) => new {c, index})
.GroupBy(x => x.index/splitByCount)
.Select(group => group.Select(elem => elem.c))
.Select(chars => new string(chars.ToArray()));
Console.WriteLine(string.Join(",", split));
the output
DV,MN,KL,DV,MN,KL

Populate select box from database using MVC [closed]

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)

How to search keywords using "And" clause in database : LINQToSQL [closed]

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
I developed a search functionality recently which searches database on EnglishValue column in Tag table. I recieve list of Keywords from search box and i search it using following query and it gives me correct results.
List<string> keywords = new List<String>(searchText);
IQueryable<Tag> tags = from p in db.Tags
where keywords.Contains(p.EnglishValue)
select p;
var matchingTagList = tags.ToList();
Now one of the ask is to search using "AND", currently LINQ query is doing OR. If any one can give suggestions how to do AND search using LINQToSQL that will help.
I am adding more details on how my database looks like. If i seach for Pet-Friendly and Wi-Fi than i should get Id= 1.
Try this:
List<string> keywords = new List<String>(searchText);
IQueryable<Tag> tags = from p in db.Tags
where keywords.All(x=> p.EnglishValue.Contains(x))
select p;
var matchingTagList = tags.ToList();
Simply try this. That why I love LINQ. Assuming you keyword is containing the term and also some varialble here called Num =5;
List<string> keywords = new List<String>(searchText);
IQueryable<Tag> tags = from p in db.Tags
where keywords.Contains(p.EnglishValue) && p.num==5
select p;
var matchingTagList = tags.ToList();

dynamic list in C# [closed]

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
if(some condition)
{
var columnSeries = (from l in logs
group l by l.MonitoringPorfileName into grp
select new
{
type = "column",
name = grp.Key,
data = (from h in Hours
let gd = grp.Where(x => x.Hours == h)
select gd.Sum(x => x.Count)).ToArray()
}).ToList();
}
}
How can i make this variable columnSeries a global variable? i have searched a lot on this, found list dynamic> new{}; but none of them are working so help will be really appreciated
Make the anonymous type you want a class.
public class ColumnSeries
{
public string type {get; set;}
//...
}
//class level variable
IEnumerable<ColumnSeries> columnSeries = null;
//then create the ColumnSeries list
columnSeries = (from l in logs
group l by l.MonitoringPorfileName into grp
select new ColumnSeries
{
type = "column",
name = grp.Key,
data = (from h in Hours
let gd = grp.Where(x => x.Hours == h)
select gd.Sum(x => x.Count)).ToArray()
});
You are creating an anonymous type inside your if statment but you want to use the result ouside the scope of the if statement. Usually you just define 'columnSeries' before the if BUT this is an anonymous type so it's not obvous.
So, before the if statement do the following (untested but should be close):
var columnSeries = Enumerable.Repeat(new {type="", name="", data=new int[]{0}}, 0);
Check out this question for more info

Categories

Resources