C# LINQ Select table from list with multiple fields

asp.net-core c# entity-framework-core linq

Question

I'd like to select many values from a table with a list. I have FabricTable(Year is int):

+----+-------+---------+------+
| Id | Color | Texture | Year |
+----+-------+---------+------+
| 1  | Red   | Rough   | 2019 |
+----+-------+---------+------+
| 2  | Green | Soft    | 2019 |
+----+-------+---------+------+
| 3  | Blue  | Rough   | 2019 |
+----+-------+---------+------+
| 4  | Red   | Med     | 2019 |
+----+-------+---------+------+
| 5  | Blue  | Soft    | 2018 |
+----+-------+---------+------+

I have selectedItems list (year is int):

+---------+------+
| Texture | Year |
+---------+------+
| Rough   | 2019 |
+---------+------+
| Soft    | 2019 |
+---------+------+

I'd like to get the Id from table, it should result with Id = 1, 2, & 3.

How can I achieve this with Linq in C#? I just need to select by Texture & Year

Here's what I've tried but I'm not sure how to select from list with multiple values(selectedItems is a list but I don't know how to query multiple columns):

db.FabricTable.Select(o => o.Texture == selectedItems.Texture && o.Year == selectItems.Year)
1
0
7/19/2019 4:40:36 PM

Accepted Answer

You get a compiler error when using selectedItems.Texture because selectedItem is a list that contains an object with the Texture property. You need to check all of the items in the list when searching for the desired items in FabricTable:

var items = db.FabricTable.Where(o => selectedItems.Any(selectedItem => o.Texture == selectedItem.Texture && o.Year == selectedItem.Year));
2
7/19/2019 4:43:30 PM

Popular Answer

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication120
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Item> items = new List<Item>() { 
                new Item() { Id = 1, Color = "Red", Texture = "Rough",  Year = 2019},
                new Item() { Id = 2, Color = "Green", Texture = "Soft",  Year = 2019},
                new Item() { Id = 3, Color = "Blue", Texture = "Rough",  Year = 2019},
                new Item() { Id = 4, Color = "Red", Texture = "Soft",  Year = 2018}
            };
            DataTable dt = new DataTable();
            dt.Columns.Add("Color", typeof(string));
            dt.Columns.Add("Texture", typeof(string));
            dt.Columns.Add("Year", typeof(int));

            foreach (Item item in items.Where(x => (x.Texture == "Rough") && (x.Year == 2019)))
            {
                dt.Rows.Add(new object[] { item.Color, item.Texture, item.Year });
            }

        }

    }
    public class Item
    {
        public int Id { get; set; }
        public string Color { get; set; }
        public string Texture { get; set; }
        public int Year { get; set; }
    }

}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow