Compare DB Item with an List<string> Input [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 4 years ago.
Improve this question
This is the data from my input
{Test,Sample}
This is the data from my database
{C:\Users\Test,D:\DriveB\Sample\Test,C:\Users\Private\Item\LocationB}
My expected output is
{C:\Users\Test,D:\DriveB\Sample\Test}
How can I get results out using that input? So far I have tried using a for loop like this
var Count = Input.Count;
for(var i = 0; i < Count; i++)
{
data = data.Where(u => u.Location.Contains(Input[i])).ToList();
}
The variable data has already been extracted from the database and has a format like this
Id
Name
Location
FileType
But the problem is that when it goes past the first loop, it immediately eliminates the second inputs data.

var Count = Input.Count;
data = data.Where(x => input.Any(y => x.Location.Contains(y))).ToList();
}
would likely work. The use of Any means:
Get me any row from data where at least one of the entries from input is contained in (i.e. a substring of) the Location.

Use split by "," first
var input = new List<String>() { "Test", "Sample" };
var db = #"C:\Users\Test,D:\DriveB\Sample\Test,C:\Users\Private\Item\LocationB";
var result = db.Split(',').Where(p => input.Any(c => p.Contains(c))).ToList();

Related

How do I turn a list of words into a flattened list of characters 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 1 year ago.
Improve this question
I have a list of string like this:
List<string> KeyList=["AD","B","KT"]
I want to split each item in the list in such a way where the final list output is a list of strings with only 1 character.
e.g.
["A","D","B","K","T"]
I have tried doing KeyList.ToCharArray() but that does not work.
You could use the Select LINQ extension method to turn each string into a list of 1-character strings, then flatten the list with SelectMany:
var list = new List<string>{ "AB", "B", "KT" };
var newList = list.SelectMany(s => s.Select(c => new string(c, 1))).ToList();

Generate unique name for multiple worksheets in a for loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am using for loop to generate some worksheets, and I want to give each one a unique name. All I get is Sheet1, Sheet2, Sheet3, and so on.
Below is my code:
var package = new ExcelPackage();
for (var i = 0; i < ds.Tables.Count; i++)
{
var ws = package.Workbook.Worksheets.Add(String.Format("Sheet{0}", i));
ws.Cells["A1"].LoadFromDataTable(i == 0
? Transpose(ds.Tables[i].Copy()).DefaultView.ToTable()
: ds.Tables[i], true, TableStyles.Medium1);
ws.Cells[ws.Dimension.Address].AutoFitColumns();
ws.Cells["A:J"].Style.Numberformat.Format = "#,##0";
}
You haven't shown where the names are coming from, but probably the best way would be to use the name from some field in the same data source that you're using to populate the sheet.
Here's one one way to give them each unique names would be to have them stored in a list, which you can access using the same index that you're using currently. Of course you have to somehow ensure that the names are in the correct order in your list:
var sheetNames = new List<string> { "Summary", "EmployeeData", "Benefits" };
for (var i = 0; i < ds.Tables.Count; i++)
{
// Choose a name from the list or use 'Sheet1, 2, 3' if we don't have enough names
var sheetName = i < sheetNames.Count
? sheetNames[i]
: String.Format("Sheet{0}", sheetNames.Count - i);
var ws = package.Workbook.Worksheets.Add(sheetName);
In this line of code here:
var ws = package.Workbook.Worksheets.Add(String.Format("Sheet{0}", i));
You're setting the worksheet name, that's why you end up with 'Sheet1', 'Sheet2', etc.
Changing that to something else, you get your worksheets named differently.
Now, I'm not sure, if your problem is where you can change the name or how you can do it, to make it unique. Depending on what you're aiming for, you could use Guid's.

How to remove null values elements from model 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 6 years ago.
Improve this question
I try to Extract data using LINQ query and adding into a model list.In this list only I need some of the fields values, but it's returning all the table fields include with null values. I have use model first approach so all the model classes will create the get set methods with all the table fields name so I want to remove some fields when I am retrieving the data Is it possible??
This my code
for (var z = 0; z < room_ament_list.Count(); z++)
{
var deal_room_amentity_id = room_ament_list[z];
var deal_room_amentity_details = db.deal_room_amentity.Where(a => a.room_amenity_id == deal_room_amentity_id).ToList();
foreach (var item_r_amentity in deal_room_amentity_details)
{
deal_room_amentity_list.Add(new deal_room_amentity()
{
room_amenity_id = item_r_amentity.room_amenity_id,
amenity_type = item_r_amentity.amenity_type,
});
}
}
this my table structure
And this is returning data from that list
But I need to Extract the data of what I have assigned in the list(without null elements).Is it possible to remove those null value element from the list??
If I am understanding your question correctly, you could modify you LINQ query as follows:
var deal_room_amentity_details = db.deal_room_amentity
.Where(a => a.room_amenity_id == deal_room_amentity_id
&& a.room_amenity_id != null).ToList();
You can download just the information you need from a SQL table by specifying an object definition in your Linq query.
var data = from x in db.deal_room_amentity
where
x.room_amenity_id == deal_room_amentity_id
select new {
id = x.id,
room_amenity_id = x.room_amenity_id
};
foreach(var item_r_amentity in data){
deal_room_amentity_list.Add(new deal_room_amentity()
{
room_amenity_id = item_r_amentity.room_amenity_id,
amenity_type = item_r_amentity.amenity_type,
});
}

how i can get the data that stored in list [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 7 years ago.
Improve this question
I want to generate a random number from first list (list of Object) and put it in the second list to get a random connection id to make connection between the original id and the random id how I can get the item from the first list by index and of which type I have to cast it
public class OneHub :Hub
{
static List<UserId> ConnectedUser = new List<UserId>();
static List<MessageDetail> CurrentMessage = new List<MessageDetail>();
static List<ConnectionsId> Connection = new List<ConnectionsId>();
public void Connect(string id)
{
if (ConnectedUser.Count(x => x.ConnectionId == id) == 0)
{
ConnectedUser.Add(new UserId { ConnectionId = id });
if (ConnectedUser.Count != 0 || ConnectedUser.Count != 1)
{
Random r = new Random();
int x = r.Next(0,ConnectedUser.Count);
(object)ConnectedUser.item[x];
Connection.Add(new ConnectionsId {ConnectionId=id,ConnectionId2= })
}}}
First off, you're going to need to make sure that the ConnectedUser that you randomly get is not the same user you are linking to, before you add that connection, or you're going to find further issues.
For ConnectedUser, you can get the index by simply using ConnectedUser[x]. (I suggest making your lists plural so it's obvious that they're collections.)
You need to assign that connected user to a new object.
Something like
UserID linkedUser = ConnectedUser[x];
This way, you can reference linkedUser.ConnectionId in your connection addition.
Alternately, you could just use:
Connection.Add(new ConnectionsId { ConnectionId = id, ConnectionId2 = ConnectedUser[x].ConnectionId };
This random setup, though, does have a strong potential for having several people ending up not linked to anyone. Additionally, your line that states:
if (ConnectedUser.Count != 0 ...
is redundant. You just added a user to that list. It should never be of size 0.

export linq query to a text file [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 7 years ago.
Improve this question
I want to make a text file from a linq query.
The point is that my query produces more than 3000 records each time and when I use foreach it takes 2 minutes to extract all records and fields data.
foreach (var query3 in query)
{
line += (string)query3.Name.Trim() +"\t"+(string)query3.Code.Trim()+"\n";
}
How can I export linq query to a text file or string variable??
my query is something like :
var Query=from c in db.Exp select c;
You shouldn't use string when you perform a lot of string appending. Since string is an immutable object, a new block of memory is allocated in each iteration. You should use StringBuilder instead:
StringBuilder line = new StringBuilder();
foreach (var q2 in query)
{
line.AppendFormat("{0}\t{1}\n", query3.Name.Trim(), query3.Code.Trim());
}
File.WriteAllText(#"...", line.ToString());

Categories