table:ProductComapny
field:NameProduct
NameProduct
1
1
1
4
5
i want update fields NameProduct where NameProduct= 1 to(NameProduct=1,NameProduct=2,NameProduct=3
how can this work??
result
NameProduct
1
2
3
4
5
var source = new int[] {1,1,1,4,5};
var result = source.Select( (val,i) => val==1 ? i : val );
Try This:
using (DataClassesDataContext dc=new DataClassesDataContext())
{
var my = from a in dc.ProductComapny
where a.NameProduct== 1
select a;
int i=1;
foreach (var item in my)
{
item.NameProduct= i;
dc.SubmitChanges();
i++;
}
}
Related
I have a LogData List and which is designed like below.
public class LogDataEntity
{
public string IndexPattern;
public int Type;
public LogDataEntity(string pattern , int type)
{
IndexPattern = pattern;
Type = type;
}
}
List<LogDataEntity> list = new List<LogDataEntity>();
list.add(new LogDataEntity("1,2,9,10", 2));
list.add(new LogDataEntity("1,10", 1));
list.add(new LogDataEntity("1,2,3", 2));
list.add(new LogDataEntity("3,9,10", 3));
list.add(new LogDataEntity("9,10", 2));
list.add(new LogDataEntity("9,10", 2));
And i want the result like below.
[Index] [Type] [Count]
10 : 2 3
9 : 2 3
1 : 2 2
3 : 1 2
2 : 2 2
3 : 3 1
9 : 3 1
10 : 3 1
1 : 1 1
I want to group by and count not only splited string(indexpattern) but also
type too. And i want to count and show them by OrderByDescending(Count).
I think There is multiple group by.
How should i do this with Linq?
You can use SelectMany to create list of (Index, Type) pairs, then group by and count to do the rest:
var pairs = data.SelectMany(x => x.IndexPattern
.Split(",")
.Select(y => new {Index = y, Type = x.Type});
var res = from p in pairs
group p by new { p.Index, p.Type } into grp
select new {
Index = grp.Key.Index,
grp.Key.Type,
grp.Count()
};
(An order by clause can be added before the final Select as required.)
You've, probably, stuck in SelectMany; all the other commands are quite evident:
var result = list
.SelectMany(record => record
.IndexPattern
.Split(',')
.Select(item => {
index = item,
type = record.Type,
}))
.GroupBy(item => item)
.OrderByDescending(chunk => chunk.Count())
.Select(chunk => $"{chunk.index,-10} : {chunk.type,-10} {chunk.Count()}");
Console.WriteLine(string.Join(Environment.NewLine, result));
This is improve version or previous answers.
var pairs = Logs.SelectMany(x => x.IndexPattern.Split(',').Select(y => new {
Index = y, Type= x.Type }));
var pairs2 = (from p in pairs group p by p into grp select new { Index =
grp.Key.Index, Reason = grp.Key.Type, Count = grp.Count() }
).OrderByDescending(p => p.Count);
foreach (var i in pairs2)
{
//print with i
}
Given the following dataset:
WharehouseId Sku OnHold InStock
===========================================
1 ABC-123 N 20
2 ABC-123 N 13
3 ABC-123 Y 4
4 ABC-123 N 18
I need to create an int[] array that returns the InStock items, but the value should be 0 if OnHold equals 'Y'. So in the dataset above, the array result should be:
{ 20, 13, 0, 18 }
I am able to accomplish this by the following:
int[] inStockQty = new int[4];
int i = 0;
foreach (var item in query)
{
inStockQty[i] = item.OnHold == 'N' ? item.InStock : 0;
i++;
}
But I'm wondering if there is also a way to do this using LINQ's ToArray()?
You can move the conditional into LINQ's Select, like this:
var inStockQty = query.Select(item => item.OnHold == 'N' ? item.InStock : 0).ToArray();
I have two datatables like this:
dt1:
ID1
----------
1
2
3
4
5
6
7
8
9
10
dt2:
ID2
----------
1
2
3
4
5
Now, I want to retrieve all combinations of the items from these two datatables such that the result will contain 50 (10 x 5) rows - something like this:
dtResult:
ID1 ID2
------------
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
2 5
3 1
. .
. .
. .
is there any simple way instead of using a loop?
You are finding for cartesian product. Use CROSS JOIN
Select a.ID1, b.ID2
FROM dt1 A CROSS JOIN dt2 B
Do a full join:
Select a.ID1, b.ID2 FROM dt1 A,dt2 B
LINQ:
var combinedRows = from a in dt1.AsEnumerable()
from b in dt2.AsEnumerable()
select new { ColumnID1 = a["ID1"], ColumnID2 = b["ID2"] };
foreach (var item in combinedRows)
{
row = dt3.NewRow();
row["ID1"] = item.ColumnID1;
row["ID2"] = item.ColumnID2;
dt3.Rows.Add(row);
}
Use
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] {
new DataColumn("ID1") });
for (int i = 0; i < 10; i++)
dt1.Rows.Add(i + 1);
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[] {
new DataColumn("ID2") });
for (int i = 0; i < 5; i++)
dt2.Rows.Add(i + 1);
var queryOne = from row in dt1.AsEnumerable()
from row1 in dt2.AsEnumerable()
select new
{
id1 = row.Field<string>("ID1"),
id2 = row1.Field<string>("ID2")
};
var result = queryOne.ToList();
Thanks all but i have solved it by Foreach method in LINQ.
Okay, so this seems simple, but I can't think of a straightforward solution;
Basically I have an object array in C# that contains, say, 102 elements. I then also have 4 other empty arrays. I want to iterate through the original array and distribute the 100 elements evenly, then distribute 101 and 102 to the 1st and 2nd new arrays respectively.
int i = 1,a=0, b=0, c=0, d = 0;
foreach (ReviewStatus data in routingData)
{
if (i == 1)
{
threadOneWork[a] = data;
a++;
}
if (i == 2)
{
threadTwoWork[b] = data;
b++;
}
if (i == 3)
{
threadThreeWork[c] = data;
c++;
}
if (i == 4)
{
threadFourWork[d] = data;
d++;
i = 0;
}
i++;
}
Now the above code definitely works, but I was curious, does anybody know of a better way to do this??
var workArrays = new[] {
threadOneWork,
threadTwoWork,
threadThreeWork,
threadFourWork,
};
for(int i=0; i<routingData.Length; i++) {
workArrays[i%4][i/4] = routingData[i];
}
Put the four arrays into an array of arrays, and use i%4 as an index. Assuming that thread###Work arrays have enough space to store the data, you can do this:
var tw = new[] {threadOneWork, threadTwoWork, threadThreeWork, threadFourWork};
var i = 0;
foreach (ReviewStatus data in routingData) {
tw[i%4][i/tw.Length] = data;
i++;
}
Linq is your friend! Use modulo to group the items via the total number of arrays in your case 4.
For example the code splits them up into four different lists:
var Items = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Items.Select( ( i, index ) => new {
category = index % 4,
value = i
} )
.GroupBy( itm => itm.category, itm => itm.value )
.ToList()
.ForEach( gr => Console.WriteLine("Group {0} : {1}", gr.Key, string.Join(",", gr)));
/* output
Group 0 : 1,5,9
Group 1 : 2,6,10
Group 2 : 3,7
Group 3 : 4,8
*/
I have a table with data similar to below:
Group TimePoint Value
1 0 1
1 0 2
1 0 3
1 1 3
1 1 5
I want to project a table as such:
Group TimePoint AverageValue
1 0 2
1 1 4
EDIT: The data is in a datatable.
Anybody any ideas how this can be done with LINQ or otherwise?
Thanks.
You need to perform Group By
The linq you need is something like:
var query = from item in inputTable
group item by new { Group = item.Group, TimePoint = item.TimePoint } into grouped
select new
{
Group = grouped.Key.Group,
TimePoint = grouped.Key.TimePoint,
AverageValue = grouped.Average(x => x.Value)
} ;
For more Linq samples, I highly recommend the 101 Linq samples page - http://msdn.microsoft.com/en-us/vcsharp/aa336747#avgGrouped
Here's a more function-oriented approach (the way I prefer it). The first line won't compile, so fill it in with your data instead.
var items = new[] { new { Group = 1, TimePoint = 0, Value = 1} ... };
var answer = items.GroupBy(x => new { TimePoint = x.TimePoint, Group = x.Group })
.Select(x => new {
Group = x.Key.Group,
TimePoint = x.Key.TimePoint,
AverageValue = x.Average(y => y.Value),
}
);
You can do:
IEnumerable<MyClass> table = ...
var query = from item in table
group item by new { item.Group, item.TimePoint } into g
select new
{
g.Key.Group,
g.Key.TimePoint,
AverageValue = g.Average(i => i.Value)
};
Assuming a class like this:
public class Record
{
public int Group {get;set;}
public int TimePoint {get;set;}
public int Value {get;set;}
}
var groupAverage = from r in records
group r by new { r.Group, r.TimePoint } into groups
select new
{
Group = groups.Key.Group,
TimePoint = groups.Key.TimePoint,
AverageValue = groups.Average(rec => rec.Value)
};