I'm currently working on a control which will allow me to assign sizes to placeholders on a page. The control working using various data tables and so is getting quite complex.
I want to make the process of creating a layout as automated as possible.
What I am trying to do is generate a number of gridview rows based on a value being pulled from a datatable.
Here is my code so far
List<GridData> Grids = new List<GridData>();
Grids.Add(new GridData { Size = 1, Title = "Grid_1" });
Grids.Add(new GridData { Size = 2, Title = "Grid_2" });
Grids.Add(new GridData { Size = 3, Title = "Grid_3" });
Grids.Add(new GridData { Size = 4, Title = "Grid_4" });
Grids.Add(new GridData { Size = 5, Title = "Grid_5" });
Grids.Add(new GridData { Size = 6, Title = "Grid_6" });
Grids.Add(new GridData { Size = 7, Title = "Grid_7" });
Grids.Add(new GridData { Size = 8, Title = "Grid_8" });
Grids.Add(new GridData { Size = 9, Title = "Grid_9" });
Grids.Add(new GridData { Size = 10, Title = "Grid_10" });
Grids.Add(new GridData { Size = 11, Title = "Grid_11" });
Grids.Add(new GridData { Size = 12, Title = "Grid_12" });
GV_PlaceHolders.DataSource = from x in db.DT_Layouts2s
where x.LayoutID == int.Parse(DDL_Layouts.SelectedValue.ToString())
select new
{
x.LayoutID,
x.PlaceHolders,
Grids,
};
GV_PlaceHolders.DataBind();
Now the way I want to generate the rows is by calling upon the place holders int value but I have no idea where even start with generating dynamic rows based on a value.
Can anyone point me in the right direction???
Cheers
Related
I want to delete it with the desired id.
Can you tell me if there is another way to do it with Linq?
I think I can use Select() and Where(), but it doesn't work.
var list = new List<object>() { };
list.Add(new { id = 3, const = "22"});
list.Add(new { id = 4, const = "22"});
list.Add(new { id = 6, const = "22"});
list.Add(new { id = 2, const = "22"});
list.Add(new { id = 1, const = "22"});
//example
list.Remove(new { id = 2, const = "22" });
first of all const is a keyword in C#, so either avoid using it as member name, or escape it by decorating with #.
since your list item type is object, you need to cast them as dynamic to allow access the 'unknown' id -> this a very vague approach, but it works.
= in C# means: set the value of... so if you want to compare equality, you need ==
putting this altogether:
var list = new List<object>();
list.Add(new { id = 4, #const = "22" });
list.Add(new { id = 4, #const = "22" });
list.Add(new { id = 6, #const = "22" });
list.Add(new { id = 2, #const = "22" });
list.Add(new { id = 1, #const = "22" });
list.RemoveAll(i => ((dynamic)i).id == 2);
will work.
Consider using an own Type for your list items, anonymous types should only be used 'locally' in Queries like GroupBy.
you could try:
list.RemoveAt(list.FindIndex(item => item.id == 1));
I am updating older code but part of it must stay the same. I have now picker that needs to be filled with list.
My list
public List<TimeoutBetweenSentences> FillTimoutOptions()
{
var newListTimeoutBetweenSentenceses = new List<TimeoutBetweenSentences>()
{
new TimeoutBetweenSentences()
{
Position = 0,
Text = "+ 0 sekund",
Value = 0
},
new TimeoutBetweenSentences()
{
Position = 1,
Text = "+ 1 sekunda",
Value = 1
},
new TimeoutBetweenSentences()
{
Position = 2,
Text = "+ 2 sekundy",
Value = 2
},
new TimeoutBetweenSentences()
{
Position = 3,
Text = "+ 3 sekundy",
Value = 3
},
new TimeoutBetweenSentences()
{
Position = 4,
Text = "+ 4 sekundy",
Value = 4
},
new TimeoutBetweenSentences()
{
Position = 5,
Text = "+ 5 sekund",
Value = 5
},
};
return newListTimeoutBetweenSentenceses;
}
List<TimeoutBetweenSentences> allOptions = FillTimoutOptions();
sentencePausesStepper.Items.Add(allOptions.Select(m => m.Text).ToList().ToString());
however this displays just as "System collections" DO zou have any idea?
this is adding an entire list as ONE element
sentencePausesStepper.Items.Add(allOptions.Select(m => m.Text).ToList().ToString());
to add elements of one list to another, use AddRange instead
sentencePausesStepper.Items.AddRange(allOptions.Select(m => m.Text).ToList().ToString());
or better, do this
sentencePausesStepper.ItemsSource = allOptions;
sentencePausesStepper.ItemDisplayBinding = new Binding("Text");
I have a simple table in the following structure.
I want to write a LINQ expression to fetch only 5 records always. This 5 should be "Gold" if available. Otherwise add "Bronze" to make it 5. If it still not 5 then add "Silver" to the list. But total results returned should be 5. It should be good in terms of performance.
I tried basic linq but no luck. Any help is highly appreciated.
Class :
public class Option {
public int Id {get;set;
public string Name {get;set;}
public string Priority {get;set;}
}
dbContext.Options would create a connection to database table through ORM and we can apply linq expressions there.
Attempt : dbContext.Options.OrderByDescending(o => o.Priority).GroupBy(a => a.Priority)
this returns grouped result by priority. But i want to include the logic i needed inside this expression.
You want to assign a sort value to each string so that they are ordered. You can do this by assigning the integer 0 to Gold, 1 to Bronze, and 2 for Silver (other).
You then use Take to just get the first 5 records.
// ordered by gold, bronze, silver
var result = dbContext.Options
.OrderBy(o => o.Priority == "Gold" ? 0 : o.Priority == "Bronze" ? 1 : 2)
.Take(5)
.ToList();
It should be good in terms of performance.
Then you could consider using raw SQL to filter the records in the original query that is executed against the database, e.g.:
dbContext.Options.SqlQuery("SELECT TOP 5 * FROM [Option] ORDER BY CASE WHEN [Priority] = 'Gold' THEN 1 WHEN [Priority] = 'Bronze' THEN 2 WHEN [Priority] = 'Silver' THEN 3 ELSE 4 END").ToArray();
Maximal performance and LINQ seldom go hand in hand when it comes to querying databases.
Let Priority be an enum, orderby it and take 5.
class Option
{
public int Id { get; set; }
public string Name { get; set; }
public Priority Priority { get; set; }
}
enum Priority
{
Gold = 0,
Silver,
Bronze
}
static void Main(string[] args)
{
var list = new List<Option>()
{
new Option { Id = 1, Name = "Bob", Priority = Priority.Gold },
new Option { Id = 2, Name = "Rob", Priority = Priority.Gold },
new Option { Id = 2, Name = "David", Priority = Priority.Bronze },
new Option { Id = 2, Name = "Adam", Priority = Priority.Bronze },
new Option { Id = 2, Name = "Jack", Priority = Priority.Silver },
new Option { Id = 2, Name = "Josh", Priority = Priority.Silver },
new Option { Id = 2, Name = "Peter", Priority = Priority.Silver },
new Option { Id = 2, Name = "Max", Priority = Priority.Silver },
new Option { Id = 2, Name = "Steve", Priority = Priority.Silver },
};
var newList = list.OrderBy(l => l.Priority).Take(5);
}
List<Option> top5 = participants.OrderBy(part => {
switch(part.Priority) {
case "Gold": return 1;
case "Bronze": return 2;
case "Silver": return 3;
default: return 4;
}
}).Take(5).ToList();
If the list is shorter than 5, just order them, if that is needed.
See code below :
List<string> rank = new List<string>() { "Gold", "Bronze","Silver" };
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Priority", typeof(string));
dt.Rows.Add(new object[] { 9, "Steve", "Silver" });
dt.Rows.Add(new object[] { 8, "Max", "Silver" });
dt.Rows.Add(new object[] { 7, "Peter", "Silver" });
dt.Rows.Add(new object[] { 6, "Josh", "Silver" });
dt.Rows.Add(new object[] { 5, "Jack", "Bronze" });
dt.Rows.Add(new object[] { 4, "Adam", "Bronze" });
dt.Rows.Add(new object[] { 3, "David", "Gold" });
dt.Rows.Add(new object[] { 1, "Bob", "Gold" });
dt.Rows.Add(new object[] { 2, "Rob", "Gold" });
DataRow[] results = dt.AsEnumerable().OrderBy(x => rank.IndexOf(x.Field<string>("Priority"))).Take(5).ToArray();
I need to perform a pretty complex MongoDB query and I'm having a really hard time being able to narrow the entire thing down to one query, although I do think it's doable I don't really have enough experience with MongoDB to get it quite right and I'd really appreciate some help.
My class looks something like this:
class MyItem
{
public int ID { get; set; }
public int Value { get; set; }
public bool IsDropped { get; set; }
}
I need to be able to select the min value for each ID that isn't dropped. For example:
items.Add(new MyItem() { ID = 1, Value = 100, IsDropped = true });
items.Add(new MyItem() { ID = 1, Value = 150, IsDropped = false });
items.Add(new MyItem() { ID = 1, Value = 200, IsDropped = true });
items.Add(new MyItem() { ID = 2, Value = 100, IsDropped = false });
items.Add(new MyItem() { ID = 2, Value = 250, IsDropped = false });
For these items, the values I want returned are:
ID: 1, Value: 150, IsDropped: false
ID: 2, Value: 100, IsDropped: false
However, if all values for a certain ID are dropped, I want to be able to know that as well so for example for these values:
items.Add(new MyItem() { ID = 2, Value = 100, IsDropped = true });
items.Add(new MyItem() { ID = 2, Value = 150, IsDropped = true });
I want to get:
ID: 2, Value: (doesn't really matter), IsDropped: true
Also on top of that, I need to be able to perform simple filter queries for example "only return items where ID == 1 || ID == 2"
Can this be done in a single query? I'm able to aggregate the class based on minimum value but adding the IsDropped parameter into the mix is making it really hard for me to write a single query that can perform all of this.
Thanks in advance for the help.
I think this can help you:
var groupBy = new BsonDocument
{
{"_id", "$ID"},
{
"min", new BsonDocument("$min", new BsonDocument
{
{"IsDropped", "$IsDropped"}, //This line will do the trick ;)
{"Value", "$Value"}
})
}
};
var results = collection.Aggregate().Group(groupBy).ToList();
And to add a filter over grouping results use this:
// `where ID == 1 || ID == 2` is as same as `where ID IN (1,2)`
var having = Builders<BsonDocument>.Filter.In("_id", new[] { 1, 2 });
// Now put having after groupBy
var results = collection.Aggregate().Group(groupBy).Match(having).ToList();
I'm hoping to get a quick solution to this.
Here is a snippet of the code that works when creating a series:
Series SeriesABC2 = new Series
{
Name = '#' + HakuAlueet[1],
Data = new Data(
new object[]{
new[] { 300, 440 },
new[] { 400, 540 }
}),
PlotOptionsArearange = new PlotOptionsArearange { Visible = true, LineWidth = 0, FillOpacity = 0.3 },
Type = ChartTypes.Arearange
};
Now the plan is to make this dynamic creating the values in the object "new[] { 300, 440 }, new[] { 400, 540 }" inside .net C#. I'm quite new to C# so I'm not quite sure of the naming convention, but I seem to need to make an array object of an two value arrays. How would I go about this?
I've tried countless ways of managing this but just haven't found a solution that would be acceptable to dotnet.Highcharts. Thanks!
string[] books= source.Select(a => a.bookName).ToArray();
object[] visits = source.Select(a => (object)a.Visits).ToArray();
DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Bar,
Width = 900
}).SetPlotOptions(new PlotOptions {
Bar = new PlotOptionsBar {
ColorByPoint = true,
DataLabels = new PlotOptionsBarDataLabels {
Enabled = true }
}
}).SetXAxis(new XAxis { Categories = books,
}).SetSeries(new Series
{
Data = new Data(visits),
Name = "User Book Visits"
});
lb_chart.Text = chart.ToHtmlString();
Here source contain data where I am querying some dynamic content. This is working and hope this will also help you.