0

I'm using LLBLGEN Pro Runtime Framwork and getting an entity of a table. Is it possible to convert this query to LINQ expression?

SELECT s1.SETTING_NAME, s1.SETTING_VALUE, s1.ROW_LST_UPD_TS, s1.MACHINE_NAME, s1.ROW_LST_UPD_UID
FROM EVENT_MGT.CONFIGURATION_LOG s1
INNER JOIN (
    SELECT SETTING_NAME, MAX(ROW_LST_UPD_TS) ROW_LST_UPD_TS
    FROM EVENT_MGT.CONFIGURATION_LOG
    WHERE APP_ID = 173
--    and ROW_LST_UPD_TS >= CURRENT_TIMESTAMP - 7
    GROUP BY SETTING_NAME
) s2 ON s1.SETTING_NAME = s2.SETTING_NAME AND s1.ROW_LST_UPD_TS = s2.ROW_LST_UPD_TS
WHERE s1.APP_ID = 173;

I tried so far this:

var log1 = config_log.Where(app => app.AppId == 173)
                     .Select(s => new {
                          s.SettingName, 
                          s.SettingValue, 
                          s.RowLstUpdTs, 
                          s.MachineName, 
                          s.RowLstUpdUid})
                     .ToList()
                     .GroupBy(x => x.SettingName); 

But its far from what I need.

1
  • 3
    You'll have better luck making a view, mapping that to an entity, and querying that with LINQ. Commented Jan 6, 2020 at 17:20

3 Answers 3

2

Try something like this

   class Program
    {

        static void Main(string[] args)
        {
            int CURRENT_TIMESTAMP = 123;

            List<CONFIGURATION_LOG> logs = new List<CONFIGURATION_LOG>();
            var results = logs.Where(x => (x.APP_ID == 173) && (x.ROW_LST_UPD_TS >= CURRENT_TIMESTAMP - 7))
                .OrderByDescending(x => x.ROW_LST_UPD_TS)
                .GroupBy(x => x.SETTING_NAME)
                .Select(x => x.FirstOrDefault())
                .Select(x => new
                {
                    setting_name = x.SETTING_NAME,
                    setting_value = x.SETTING_VALUE,
                    row_lst_upd_ts = x.ROW_LST_UPD_TS,
                    machine_name = x.MACHINE_NAME,
                    row_lst_upd_uid = x.ROW_LST_UPD_UID
                }).ToList();

        }
    }
    public class EVENT_MGT
    {
        CONFIGURATION_LOG CONFIGURATION_LOG { get; set; } 
    }
    public class CONFIGURATION_LOG
    {
        public string SETTING_NAME { get; set; }
        public string SETTING_VALUE { get; set; }
        public int ROW_LST_UPD_TS { get; set; }
        public string MACHINE_NAME { get; set; }
        public string ROW_LST_UPD_UID { get; set; }
        public int APP_ID { get; set; }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

throws an exception {"Method call to 'Join' doesn't have a known mapped database function or other known handler."} of type: SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryConstructionException
Thanks to jdweng, I figured out exactly what I want:
1

You give the loss information. Maybe you want one thing such as this keywork: Entity Framework. Follow this link;

Besides, I have an idea for optimize use of call SQL query command to get data from a table includes columns with image datatype using C# without Entity Framework in 3 steps with a small example:

Step 0: (default) Create a new table. This is my table TBUsers:

CREATE TABLE [dbo].[TBUsers]
(
    [STT] [BIGINT] IDENTITY(1,1) NOT NULL,
    [HoTen] [NVARCHAR](MAX) NOT NULL,
    [MaSo] [NVARCHAR](50) NOT NULL,
    [MatKhau] [NVARCHAR](MAX) NOT NULL,
    [KhoaLop] [NVARCHAR](MAX) NOT NULL,
    [MaTheGui] [NVARCHAR](50) NOT NULL,
    [PhanQuyen] [INT] NOT NULL,
    [ChoPhepHoatDong] [BIT] NOT NULL,
    [NguoiThem] [NVARCHAR](MAX) NOT NULL,
    [NgayThem] [DATETIME] NOT NULL,
    [SoDuKhaDung] [BIGINT] NOT NULL,
    [DangGui] [BIT] NOT NULL,
    [TruyCapLanCuoi] [DATETIME] NULL,
    [ThoiGianGuiCuoi] [DATETIME] NULL,
    [HinhAnh] [IMAGE] NULL,
    [DonGia] [BIGINT] NULL,

    CONSTRAINT [PK_TBUsers] 
        PRIMARY KEY CLUSTERED ([MaTheGui] ASC)
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                          ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

Step 1: create a new class that corresponds to the table above and create 2 constructors, Users:

    public class Users
    {
        public Users()
        { }

        public Users(object sTT, object hoTen, object maSo, object matKhau, object khoaLop, object maTheGui, object phanQuyen, object choPhepHoatDong,
            object nguoiThem, object ngayThem, object soDuKhaDung, object dangGui, object truyCapLanCuoi, object thoiGianGuiCuoi, object hinhAnh,object donGia)
        {
            STT = sTT.ToString();
            HoTen = hoTen.ToString();
            MaSo = maSo.ToString();
            MatKhau = matKhau.ToString();
            KhoaLop = khoaLop.ToString();
            MaTheGui = maTheGui.ToString();
            PhanQuyen = phanQuyen.ToString();
            ChoPhepHoatDong = choPhepHoatDong.ToString();
            NguoiThem = nguoiThem.ToString();
            NgayThem = ngayThem.ToString();
            SoDuKhaDung = soDuKhaDung.ToString();
            DangGui = dangGui.ToString();
            TruyCapLanCuoi = truyCapLanCuoi.ToString();
            ThoiGianGuiCuoi = thoiGianGuiCuoi.ToString();

            HinhAnh = hinhAnh==System.DBNull.Value?null: (byte[])hinhAnh;
            DonGia = donGia.ToString();
            Color = (bool)choPhepHoatDong;
        }

        public string STT { get; set; }
        public string HoTen { get; set; }
        public string MaSo { get; set; }
        public string MatKhau { get; set; }
        public string KhoaLop { get; set; }
        public string MaTheGui { get; set; }
        public string PhanQuyen { get; set; }
        public string ChoPhepHoatDong { get; set; }
        public string NguoiThem { get; set; }
        public string NgayThem { get; set; }
        public string SoDuKhaDung { get; set; }
        public string DangGui { get; set; }
        public string TruyCapLanCuoi { get; set; }
        public string ThoiGianGuiCuoi { get; set; }
        public byte[] HinhAnh { get; set; }
        public string DonGia { get; set; }
        public bool Color { get; set; }
    }
}

Step 2: Get Users from database with the ParseUser function:

    public static Users ParseUser(DataRow row)
    {
        var stt = row["STT"];
        var hoTen = row["HoTen"];
        var maSo = row["MaSo"];
        var matKhau = row["MatKhau"];
        var khoaLop = row["KhoaLop"];
        var maTheGui = row["MaTheGui"];
        var phanQuyen = row["PhanQuyen"];
        var choPhepHoatDong = row["ChoPhepHoatDong"];
        var nguoiThem = row["ChoPhepHoatDong"];
        var ngayThem = row["ChoPhepHoatDong"];
        var soDuKhaDung = row["SoDuKhaDung"];
        var dangGui = row["DangGui"];
        var truyCapLanCuoi = row["TruyCapLanCuoi"];
        var guiLanCuoi = row["ThoiGianGuiCuoi"];
        var hinhAnh = row["HinhAnh"] == System.DBNull.Value ? null : row["HinhAnh"];
        var donGia = row["DonGia"];

        return new Users(stt, hoTen, maSo, matKhau, khoaLop, maTheGui, phanQuyen, choPhepHoatDong, nguoiThem, ngayThem, soDuKhaDung, dangGui,
            truyCapLanCuoi, guiLanCuoi, hinhAnh, donGia);
    }

Step 3: Easily take an instance of Users class with ParseUser function:

Users user = ParseUser(sqlUtility.GetDataTable($"SELECT * FROM [dbo].[TBUsers] WHERE MaSo = 'xxx' AND ChoPhepHoatDong=1;").Rows[0]);

Similarly, you can create a class to retrieve the entire Users using any collection such as List<User> or IEnumerable<User>. You can use LinQ to query with this data.

4 Comments

I wonder how this attemps to answer the question. Could you double check you are answering the right question?
This is the code in my project I did. I share the solution to solve the problem, may be OP can do something like this.
Beyond the fact that you're not showing any program elements from the original post (how could it be helpful if you don't?), the question is about converting the query to LINQ, yet you merely say "You can use LinQ to query with this data" which is not an answer to that question. Furthermore, the question is tagged for Oracle but you're using T-SQL syntax.
I just shared the problem-solving direction with my vision and hope to help him. Sorry for the inconvenience.
0

Thanks to jdweng, I figured out how to get exactly what I need:

var results = config_log.Where(x => x.AppId == 173)
                                    .OrderByDescending(x => x.RowLstUpdTs).ToList()
                                    .GroupBy(x => x.SettingName )
                                    .Select(x => new
                                    { 
                                        setting_name = x.Select(y => y.SettingName).FirstOrDefault(),
                                        setting_value = x.Select(y => y.SettingValue).FirstOrDefault(),
                                        row_lst_upd_ts = x.Select(y => y.RowLstUpdTs).FirstOrDefault(),
                                        machine_name =  x.Select(y => y.MachineName).FirstOrDefault(),
                                        row_lst_upd_uid = x.Select(y => y.RowLstUpdUid).FirstOrDefault()
                                    }).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.